Skip to content

Comment on The 5-minute Guide to C Pointersparent

Comments

Just as with pointer arithmetic (because that's what this actually is) you don't multiply by the size of its elements. This:

  array[2]
is the same as this:
  *(array + 2)
The compiler knows what the type of data the pointer refers to and can produce the byte offset itself. Also:
  "...it's always pointing at the first element of the the array"
Eh... an array can degrade into a pointer when needed, but what does the following produce?
  char arr[10];
  ??? x = &arr;
Is "x" a pointer to pointer to char? From your assessment it would seem so, but in reality the type of "x" is
  char (*)[10]
i.e., pointer to array of char 10. An array is an array, and arrays can degrade into pointer types.

Additionally, (as you know, but merely pointing out for the curious), `sizeof arr` returns the size of arr in bytes, not the size of a pointer to the first element of arr.

  > ??? x = &arr;
Does this compile? I thought an array was treated like a constant pointer, inexistent in memory so you cannot take its address, increment it, or attribute it another value. Although the point made by RegEx about sizeof, which I didn't remember, convinced me that an array is not actually a constant pointer.

To make my thoughts clear, if arr is equivalent to &arr[0], then wouldn't &arr be equivalent to &&arr[0]?

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.