c++ static array sizeof()

68 Views Asked by At

Here is some code:

int static_array[10];
cout<<"sizeof(static_array): " <<sizeof(static_array)<<endl;

int *ptr = static_array;
cout<<"sizeof(ptr): " <<sizeof(ptr)<<endl;

which outputs:

sizeof(static_array): 40
sizeof(ptr): 8

As far as I understand, in C++ arrays are pointers and the sizeof(ptr) function returns the size of the pointer. What I don't understand, if static_array is a pointer (which has to be, since I assigned it to ptr), why its size is actually 40 bytes (10 integers) instead of 8.

0

There are 0 best solutions below