编译器假设当向地址值添加1
时,实际要做的是访问该类型的下一个变量。
示例代码
#include <stdio.h>
int main(void)
{
long multiple[] = {15L, 25L, 35L, 45L};
long *p = multiple;
for(int i = 0 ; i < sizeof(multiple)/sizeof(multiple[0]) ; ++i)
printf("address p+%d (&multiple[%d]): %llu *(p+%d) value: %d
",
i, i, (unsigned long long)(p+i), i, *(p+i));
printf("
输入的 long 占用: %d 个字节.
", (int)sizeof(long));
system("pause");
return 0;
}
执行上面示例代码,得到以下结果:
address p+0 (&multiple[0]): 9697588 *(p+0) value: 15
address p+1 (&multiple[1]): 9697592 *(p+1) value: 25
address p+2 (&multiple[2]): 9697596 *(p+2) value: 35
address p+3 (&multiple[3]): 9697600 *(p+3) value: 45
输入的 long 占用: 4 个字节.