Thanks
Thanks
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...
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 4Since 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.What padding?
Unless you pack the structure with the alignment you want.