buf[6] = 9. It is an char whose numeric value is 9 (so it is the tab character).
buf is a pointer to the character buf[0]. (buf+1) is a pointer to the character buf[1].
The operator [] is equivalent to a dereference of an offset. So buf[x] equivalent to *(buf+x).
if you don't believe me on this: try using the notation x[buf], for example 6[buf] == 9 because *(6+buf) = *(buf+6) = buf[6] = 9.
So the notation (buf+1)[x] becomes *((buf+1)+x), which is equivalent to *(buf+(x+1)), which is equivalent to buf[x+1]. So (buf+1)[5] is the same thing as writing buf[1+5] which is the same thing as buf[6].
So p = (buf+1)[5]; is the same thing as writing p = buf[6]; Since buf[6]==9, this means that p==9.
Now the important thing to remember is that buf[6] is a
value. p is a pointer. So if p=9 then p is a pointer that points to the address 9, and the VALUE *p is equal to whatever is stored at that address.
So when p prints out as the number 9, you have to remember that p is not an integer, it is a pointer. a pointer points to something. It is the address of something. If p=1 then p points to the first byte in the computers memory (well technically the first byte in the current memory scheme -- and assuming that a char is 1 byte). If p=10 then p points to the 10'th byte. If p = 1024 than p points to the second kilobyte. Etc.
Lets say we left p=(buf+1)[5]; then we asked the computer to print out p[buf-9+i] where i went from 0 to 7.
well p[buf-9+i] = *(p+buf-9+i) = *(9+buf-9+i) = *(buf+ (9-9) +i) = *(buf+i)=buf[i]
CODE
#include <stdio.h>
int main()
{
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};
int i, offset;
p = (buf+1)[5];
printf("p = %d, buf[6] = %d, &buf[6] = %d\n", p, buf[6], &buf[6]);
for (i=0; i<8; ++i)
{
offset = (int)buf-9 + i;
printf("%d, ", p[offset]);
}
printf("\n");
return 0;
}
so we would expect (and indeed we will see) the output of the program to be:
1, 2, 3, 4, 5, 6, 9, 8 which is indeed the original array.