I have the following test application:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void){
char buf[512];
buf[0]= 0x1;
buf[1]= 0x2;
char *temp1 = &buf;
char *temp2 = buf;
char *temp3 = &buf[0];
printf("temp1:%p, temp2:%p, temp3:%p\n",temp1,temp2,temp3);
printf("0 = %d, %d, %d\n",temp1[0],temp2[0],temp3[0]);
printf("1 = %d, %d, %d\n",temp1[1],temp2[1],temp3[1]);
return;
}
It compiles with a warning:
gcc ./testptr.c -o testptr
./testptr.c: In function ‘main’:
./testptr.c:9: warning: initialization from incompatible pointer type
But when I run it, all three pointers behave the same.
./testptr
temp1:0x7fff3a85f220, temp2:0x7fff3a85f220, temp3:0x7fff3a85f220
0 = 1, 1, 1
1 = 2, 2, 2
I know that buf == &buf[0]
, but why does &buf == &buf[0]
? Shouldn't &buf
be a char**
?
&buf
is actually achar(*)[512]
(a pointer to a 512-itemchar
array). They're not quite the same. – Adur