#include <iostream>
using namespace std;
void GetLeastKOfNum(int arr[])
{
cout << sizeof(arr) / sizeof(int) << endl; //这个等于2?
int main(void)
{
int arr[] = { 4,5,1,6,2,7,3,8 };
cout << sizeof(arr) / sizeof(int) << endl; //这个等于8?
GetLeastKOfNum(arr);
}
问题:
原因:
因为数组做函数形参传递的只是该数组首元素的地址,函数体内测出的字节数是指针的字节数,不是数组占用的字节,sizeof只有在看到数组定义时才能够测出字节数。