9

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
2
  • Shouldn't the second line be int *ptr = arr; ? That would make it point to the start (first element of) the array, which is equivalent to &arr[0]. Commented Oct 27, 2019 at 16:48
  • 1
    @AndreasWenzel Shouldn't the second line be int *ptr = arr;? Actually, no. int (*ptr)[4] creates ptr as a pointer to a full array of four int values. Pointer syntax like that is necessary to dynamically allocate true-multidimensional arrays. The "2-dimensional arrays" created with nested malloc() loops and wrongly described as multidimensional arrays are really 1-d arrays of pointers to multiple 1-d arrays. See stackoverflow.com/questions/42094465/… Commented Oct 27, 2019 at 17:02

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

17 Comments

@Abd-ElrahmanMohamed Agree "But ptr[0][0] is an integer not points to integer ". "ptr[0] is the address of first element in the array" is not true.
@Abd-ElrahmanMohamed yes, the values are same but types are different. ptr[0] has array type and &ptr[0][0] has int * type
@chux ptr[0] (implicitly converted to int *) would evaluate to address of the first int element.
@chux about sizeof - right, I misunderstood the context of what you said
@Abd-ElrahmanMohamed That does not. printf("someforamt", ptr[0] , ptr[0]+1) does something different than sizeof(ptr[0]). The ptr[0] in the first case goes through an inplicit conversion. With sizeof(ptr[0]), ptr[0] does not.
|
5

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.

Comments

2

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)

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.