consider these two programs:
//////////////////////Program 1////////////
void print(int arr[])
{
int *p=arr;
cout<< sizeof(arr)<<endl; //// here it is 8
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr);
return 0;
}
///////////////////////////// ///////////////////Program 2///////////
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30};
printf("%ld \n",sizeof(arr)); /// here it is 12
return 0;
}
////////////
arr
has decayed to a pointer type once it's been passed toprint
. Sosizeof
within that function yields the size of a pointer toint
on your platform.In Program 2, that decay has not occurred: the type of
arr
is an array of 3int
s. The size issizeof(int) * 3
.