2017-07-18 158 views
0

我想写一个循环遍历数组中的所有元素。我学到了概念here。我在执行方面遇到一些困难。我一直在尝试调试,并且已经将下列函数编写为该调试过程的一部分。以下是我的代码:为什么sizeof()我的双数组[4]只有4?

#include <iostream> 
using namespace std; 

struct Vmul { 
    double c[4][4]; 
}; 
double Vmulti(double a[4], double d[4]) { 
    cout << sizeof(a) << endl; 
    cout << sizeof(a[0]) << endl; 
    cout << sizeof(a)/ sizeof(a[0]) << endl; 
    return 0; 
} 
int main() 
{ 
    double r[4] = { 1,2,3,4 }; 
    double q[4] = { 1,2,3,4 }; 
    Vmulti(r, q); 
    return 0; 
} 

输出:

4 
8 
0 
Press any key to continue . . . 

我无法找出原因的sizeof(A)仅返回4?不应该是8 * 4吗?为什么不是sizeof给我的大小,而是给我数组中的元素的数量?

+4

你测量“指针”的大小(用词不当数组,但几乎同样的事情),这似乎是4字节在您的机器上。 –

+1

查看https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-ap – YYC

+1

您使用'cout'和'<<'提示我,这不是一个C问题。 C和C++不是同一种语言。 –

回答

6

从编译器的错误信息可以走很长的路要走:

test.cpp:8:23: warning: sizeof on array function parameter will return size of 'double *' instead of 'double [4]' 
     [-Wsizeof-array-argument] 
     cout << sizeof(a) << endl; 
        ^
test.cpp:7:22: note: declared here 
double Vmulti(double a[4], double d[4]) { 
相关问题