Xylene23 Xylene23 - 2 months ago 7
C Question

Pointers of Array in C

#include <stdio.h>

void main()
{
int a[]={1,2,3,4};

printf("%u %u ",a ,&a); //a
printf("%d %d ", *a ,*&a); //b
}


In ath line the output of
a
and
&a
are same address but in bth line
*&a
does not give me the answer as
1
.

I know that
&a
means pointer to array of integers but as the address is same, it should print
1
right?

Answer

a decays to the pointer to the first element of the array.
&a is the pointer to the array of 4 ints.

Even though the numerical values of the two pointers are the same, the pointers are not of the same type.

Type of a (after it decays to a pointer) is int*.
Type of &a is a pointer to an array of 4 ints - int (*)[4].

Type of *a is an int.
Type of *&a is an array of 4 ints - int [4], which decays to the pointer to the first element in your expression.

The call

printf("%d %d ", *a ,*&a);

is equivalent to:

printf("%d %d ", *a , a);

BTW, You should use %p for pointers. Otherwise, you invoke undefined behavior. Increase the warning level of your compiler to avoid making such errors.