6

I know that a pointer in C language is a variable that holds an address. for example:

int myVar;
int *p;
p = &myVar;
printf("%p\n", p);
printf("%p\n", &p);

The output will be two different addresses, the first one is the address of myVar and the second one is the address of the pointer itself which means a pointer is a variable also and have an address in memory (Correct me if I'm wrong)

But my question is about arrays, for example we have this code:

int myArray[10];

so here I thought myArray is a pointer also that holding the address of the starting point of myArray which in this case is myArray[0], so when I print address of myArray[0] and myArray I get same result:

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

Now the confusing part for me is here when I try to print the address of myArray itself:

printf("%p\n", &myArray);

Here I get the same results I was expecting to get another address like pointers.Is myArray a label or something?

2

1 Answer 1

7

&myArray[0] is the address of the first element of the array, &myArray is the address of the array. These must have the same address, but are different types.

myArray is not a pointer. It is an array that decays to pointer of type int* under certain instances. Being a parameter of printf is such an instance.

1
  • Good answer! OP: try adding the following, if you want to get even more confused: printf("%p\n", *(&myArray)); and printf("%p\n", *(&myArray[0])); for maximum effect, initialise your array: int myArray[10] = { 1,2,3,4,5,6,7,8,9,10 }; Commented Sep 18, 2019 at 15:21