1
#include <stdio.h>
#include <stdlib.h>
int main() {
    char x[5] = {'h', 'e', 'l', 'l', 'o'};
    printf("x=%p", x);
    printf("\n&x=%p", &x);
    return 0;
}

shouldn't the first statement print the address of x[0] while the second statement print the address of x, i.e the address of the array x

Jai Jain
  • 41
  • 1
  • 3
  • If I’m not mistaken. You’re not accessing the first index of the array “x” the first time but rather accessing the array as a whole. It should be printf(“x=%p, x[0]). IF I’m not mistaken. – Bigbob556677 Aug 04 '18 at 03:10
  • 1
    Why do you expect address of `x[0]` to be different from address of `x`? Both `x[0]` and the whole `x` are located at the same address in memory. No wonder the printed addresses are the same. – AnT stands with Russia Aug 04 '18 at 03:17
  • Note that you get different values if you print `x + 1` and `&x + 1`. – Jonathan Leffler Aug 04 '18 at 05:23

1 Answers1

2

The address of an array is the address of the first element of that array.

In fact, the standard mandates that, except under limited circumstances, an array will decay to a pointer to its first element.

x[n] is defined as *(x+n) - if you use zero as the value of n, you should hopefully see that eqivalence

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953