hi, i have question about sizeof, when it used with pointers and arrays.
1 2
int a[] = {0,1,2};
size_t = sizeof(a);
here i will get the size of array a in bytes.
1 2
int * pa = newint[3];
size_t = sizeof(pa);
but, here i will get size of pointer to type int
my question is why?
because, pa pointers to the first element of array, just as a.
how i can get the size of an array, if i have only pointer to the first element of this array?
a is not a pointer. It's an array. Arrays are more than just pointers to an element. They have the exact same effect as creating n objects of that type. When sizeof is applied to an array within the scope the array was declared in, it returns the size of the entire array. The compiler can do that because at that point it can clearly see the type information of sizeof's operand.
how i can get the size of an array, if i have only pointer to the first element of this array?
You can't, quite simply. At least not without doing something like ending the array with an invalid element, like C strings do.
The value of an array is the address of the first element. It's value cannot be changed. The size of an array is the number of elements times the size of one element.
The size of a pointer is ... the size of a pointer.