一个关于数组长度打印的问题
我想要得到一个未知数组的长度,网上搜索的大多数方法都是采用
sizeof(a)/sizeof(<字节类型>)的方法
但是在这里产生了疑问
这里是我在vs2017中运行的代码
#include "pch.h"
#include
using namespace std;
int main()
{
int a[10];
double b[10];
cout << "10位int型变量数组 :"<< sizeof(a)
<< endl << "10位double型变量数组:" << sizeof(b) << endl;
cout << "a :" << a << endl;
cout << "a[0]:"<< &a[0] << endl;
cout << "b :" << b << endl;
cout << "b[0]:" << &b[0] << endl;
if (a == &a[0])
{
cout << "等价";
}
}
打印结果:
10位int型变量数组 :40
10位double型变量数组:80
a :004FF88C
a[0]:004FF88C
b :004FF834
b[0]:004FF834
等价
很明显,a作为数组名,作为常量指针指向了a[0],
但是为啥sizeof(a)得到的结果是40呢??