In the below two lines,
char a[5]={1,2,3,4,5};
char *ptr=(char *)(&a+1);
printf(\"%d\",*(ptr-1));
This prints 5 on screen.Whereas when use
Arrays "decay" into pointers to the first element. So taking the address of a gives you a pointer to an array of 5 chars, which is like declaring a char[][5]
. And incrementing this pointer advances to the next element of the char[][5]
array - that is 5 characters at a time. This is different from incrementing the pointer that decays from the char[5]
array - that is, one character at a time.