0

that's my first question on stackoverflow. I was reflecting on how arrays and names of arrays work and i got a doubt. I'm wondering why the address of an array name, and the address that he points to is the same. Moreover, why if i try to extrat the value at the addres pointed he returns me 0x1 and not the address itself.

Thats the code, thanks to anyone who answers.

int x[5]={1,2,3,4,5};
int y;

printf("%p %p\n", &x, x);
printf("%p %p\n", &x[0], x);
printf("%p %p", x[0], *x);

return 0;

Thats the output on CodeChef:

0x7ffc7bfa3a70 0x7ffc7bfa3a70n
0x7ffc7bfa3a70 0x7ffc7bfa3a70
0x1 0x1

Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
  • An array is just a fixed size block of memory and a pointer to the array is a pointer to the location where this memory block starts. And the first element of the array is of course also residing at this position, so a pointer to an array is also a pointer to it's first element. – derpirscher May 01 '21 at 18:01
  • `n` is not a hex digit. A possible typo? – Janez Kuhar May 01 '21 at 18:03
  • But why the address where the name is located at, and the address that he points to are tha same? – Raffaele Liguori May 01 '21 at 18:08
  • 1
    Does this answer your question? [How come an array's address is equal to its value in C?](https://stackoverflow.com/questions/2528318/how-come-an-arrays-address-is-equal-to-its-value-in-c) – Alexander Guyer May 01 '21 at 18:08
  • Thx to Alexander Guyer – Raffaele Liguori May 01 '21 at 18:16

0 Answers0