Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

And why is that? I am a certified C noob and this really confuses me. Why `alphabet_pointer = alphabet;` points exactly to the first element in the array? Why not the whole array? Why not the last? Can we have a pointer that accepts a whole array?

Thanks



Because most of the time* an array name decays to a pointer to the first element of the array.

So when the array is declared

  char alphabet[10] = {'a', 'b', 'c', 'd', 'e'};
What the alphabet variable "holds" can be seen (this is not exactly true) as a pointer to the array.

Then when you do

alphabet_pointer = alphabet;

You just assign to alphabet_pointer the position in memory of the (first element of) alphabet.

Then alphabet_pointer can be dereferenced to access the content of the array (and not the address of a pointer to the array).

__

* There are some situations where an array name is not considered as a pointer to the array. Notably &array gives you back the address of the array : &array == array.

https://stackoverflow.com/questions/17752978/exceptions-to-a...


> Why `alphabet_pointer = alphabet;` points exactly to the first element in the array? Why not the whole array?

Someone decided it has to be so, because it is pretty convenient. Usually when dealing with an array, you want to do things with its elements and therefore it's extremely convenient that an expression with array type is converted to a pointer to the array's first element. If that didn't happen, then you'd very often have one extra layer of annoyance to go through in order to access array elements.

> Why not the last?

The first element is convenient because then you can reach for the other elements by adding a zero-based offset or index to the pointer. How often do you operate on an array starting from its end? How often do you like to work with negative indices? That's why not the last.

> Can we have a pointer that accepts a whole array?

We can have a pointer that points to a whole array:

  int a[50];
  int (*p)[50] = &a;
  
  printf("%zu %zu %zu\n", sizeof a, sizeof a / sizeof *a, sizeof *a);
  printf("%zu %zu %zu\n", sizeof *p, sizeof *p / sizeof **p, sizeof **p);

  > 200 50 4
  > 200 50 4


But what is an array anyway? It's just a bunch of (contiguous) memory. That bunch of memory has to start somewhere.

Since an array is just a bunch of memory, by pointing to the beginning of that bunch of memory you are pointing to the entire array.

Here follows a more complicated version:

What the tutorial is not telling you (and now you will hate me for doing things more complicated) is that in C, the alphabet variable is (or can be) treated as a pointer.

If you print the value of alphabet as an number (casting it to unsigned int, for example), you will see that is a position in RAM. That position is the beginning of the array. When you do `alphabet_pointer = alphabet;` you assign to alphabet_pointer the value of alphabet.

If alphabet array starts at address 0x1234, then basically you are doing

    alphabet_pointer = (char*) 0x1234;
Also note that doing `alphabet_pointer = alphabet;` is the same as doing `alphabet_pointer = &alphabet[0];`, being alphabet[0] the first element in the array.


a c array is literally just a sequence of objects in memory (possibly with padding, but you can ignore that for a while). you can think of pointing "exactly" to the first element as being equivalent to pointing to the whole array. you need to know where the array begins and its size to do anything with it. but once you know where it starts, you can access the next element by adding sizeof(char) (or whatever the element type happens to be) to the pointer.


> possibly with padding

What padding?


If you declare a structure of unaligned size, the compiler might (probably must) introduce some padding bytes at the end of the structure to fit the alignment. In some architectures you cannot do unaligned accesses (ARM for example).

Unless you pack the structure with the alignment you want.


Yes, so the structure itself has padding (added by the compiler). But that is regardless of whether it is in an array or not. OP seemed to suggest that C arrays introduce padding of some sort, which they do not.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: