我试图创建一个函数来查找数组中的元素数量.为此我找到了以下代码:
#include<iostream>
#include<stdlib>
int no_of_ele(A[]) //function to return size of array
{
return (sizeof(A)/sizeof(A[0]);
}
void main()
{
system("cls");
int arr[5] = {1,2,3,4,5};
cout<<"Number of elements in array are "<<no_of_ele(arr)<<endl;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
然后,我这样做了:
cout<<"Size of array is "<<sizeof(arr)<<endl;
cout<<"Size of data type is "<<sizeof(arr[0]);
Run Code Online (Sandbox Code Playgroud)
现在我得到绝对正确的大小输出如下:
为什么?