First to understand the difference between the array and the pointer, see the following code:
int getsize (int data[])
{
return sizeof (data);
}
int _tmain (int argc, _tchar* argv[])
{
int data[] = {1,2,3,4,5};
int size1 = sizeof (data);
int *data2 = data;
int size2 = sizeof (DATA2);
int size3 = getsize (data);
cout<<size1<<endl<<size2<<endl<<size3<<endl;
GetChar ();
return 0;
}
The run output is 20,4,4.
Three conclusions can be drawn here.
1. The size of the array can be obtained by using the sizeof operator in the log group. sizeof makes the computed object occupy the memory.
2, the array name is also a pointer. (sizeof a pointer can always only get a pointer variable of memory, which is related to machine word length, 32-bit machine is 4 bytes)
Although Data2 points to the first number of the array data1, he is still a pointer.
3, the function parameter uses the array, actually is like the pointer, when calls, the array pointer automatically degenerate to the normal pointer, therefore the SIZE3 obtains is 4.
Pointers and Arrays