Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c

pointers

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
like image 972
Abd-Elrahman Mohamed Avatar asked Oct 27 '19 16:10

Abd-Elrahman Mohamed


People also ask

How does sizeof work with pointers?

The sizeof() operator returns pointer size instead of array size. The 'sizeof' operator returns size of a pointer, not of an array, when the array was passed by value to a function. In this code, the A object is an array and the sizeof(A) expression will return value 100. The B object is simply a pointer.

How does dereferencing a pointer work?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.

What happens when you dereference an array?

You cannot dereference an array, only a pointer. What's happening here is that an expression of array type, in most contexts, is implicitly converted to ("decays" to) a pointer to the first element of the array object. So ar "decays" to &ar[0] ; dereferencing that gives you the value of ar[0] , which is an int .

What happens when you dereference a pointer?

Dereference a pointer is used because of the following reasons: It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer. Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.


2 Answers

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.

like image 196
chux - Reinstate Monica Avatar answered Oct 02 '22 20:10

chux - Reinstate Monica


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.

like image 38
Volodymyr Boiko Avatar answered Oct 02 '22 18:10

Volodymyr Boiko