starting address of array a and &a

前端 未结 5 1273
我在风中等你
我在风中等你 2021-02-03 14:03

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

5条回答
  •  后悔当初
    2021-02-03 14:25

    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.

提交回复
热议问题