简体   繁体   中英

How does sizeof work with this dereferencing of a pointer to array?

Here I have a pointer ptr to array arr of 4 integers. ptr points to the whole array. ptr[0] or *ptr points to the first element of the array, so adding 1 to ptr[0] gives the address of the second element of the array.

I can't understand why using sizeof(ptr[0]) gives the size of the whole array, 16 bytes, not the size of only the first element, 4 bytes, (as ptr[0] points to the first element in the array).

int arr[4] = {0, 1, 2, 3};
int (*ptr)[4] = &arr;
printf("%zd", sizeof(ptr[0])); //output is 16

OP: ptr[0] points to the first element in the array.

Type confusion. ptr[0] is an array.

ptr is a pointer to array 4 of int .
ptr[0] , like *ptr deferences the pointer to an array .
sizeof(ptr[0]) is the size of an array.


With sizeof(ptr[0]) , ptr[0] does not incur "an expression with type ''pointer to type'' that points to the initial element of the array object" conversion. (c11dr §6.3.2.1 3). With sizeof , ptr[0] is an array.

ptr here is of type pointer to an array of 4 int elements and the array type has size 16 on your platform (sizeof(int) * (number of elemetns)).

I can't understand why using sizeof(ptr[0]) gives the size of the whole array 16 bytes not the size of only the first element 4 bytes

because C type system has array types. Here both arr and *ptr has it. What you declare that you have. To get sizeof int here you should sizeof(ptr[0][0]) - where ptr[0] evaluates to array.

with int (*ptr)[4] = &arr; you have a pointer to an array of four integers and pointing to arr.

ptr is now pointing to arr , like a double pointer. We can access elements of arr using ptr[0][x] where x could be 0 to 4 .

So sizeof(ptr[0]) is same as sizeof(arr)

By definition, ptr[0] is the same as *(ptr + 0) which in turn is the same as *ptr . Further, ptr is initialized with &arr , so *ptr is *&arr and that is just arr . Note that the intermediate storage of &arr in ptr does not perform any array decay, so the equivalence is maintained and no type information is lost.

Note that all this is computed at compile-time, just to avoid this additional pitfall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM