I was trying to print the location of array elements using the following code
#include<stdio.h>
main()
{
int arr[]={10,20,30,40,50,60};
int i;
for (i=0; i<6; i++)
printf("%d ", &arr[i]);
return 0;
}
Error: printArrayLocation.c:7:3: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
While i was googling for the solution, I came across the following codes.
for (i=0; i<6; i++)
printf("%p ", (void *) arr[i]);
Result: 0xa 0x14 0x1e 0x28 0x32 0x3c
for (i=0; i<6; i++)
printf("%p ", arr+i);
Result: 0xbf8985a4 0xbf8985a8 0xbf8985ac 0xbf8985b0 0xbf8985b4 0xbf8985b8
Both are giving results.
I know that arr points to the element with index 0, and that arr+i will point to the subsequent elements, but what is %p?
Please also explain the 2nd code(no idea what's happening)
Also, why are the results in hexadecimal form?