为什么sizeof()我的双数组[4]只有4?

时间:2017-07-18 19:46:41

标签: c++ arrays sizeof

我正在尝试编写一个遍历数组中所有元素的循环。我学会了概念here。我的执行面临一些困难。我一直在尝试调试,我已经编写了以下函数作为调试过程的一部分。以下是我的代码:

#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吗?为什么不给我大小的尺寸,而是给我数组中的元素数量?

1 个答案:

答案 0 :(得分:7)

来自编译器的错误消息可以有很长的路要走:

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]) {