C++ 指针偏移的理解

//题目:若有程序段int a[5] = { 1, 2, 3, 4, 5 }; int *p = (int *)(&a + 1); printf("%d,%d", *(a + 1), *(p - 1)); 则输出的结果是(),()


#define
_CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> int main() { int a[5] = { 1, 2, 3, 4, 5 }; int *p = (int *)(&a + 1); //&a+1的意思为 a数组地址向右偏移一个a类型的数组地址大小, (int*)(&a+1)则表示解引用, //取此地址存放的值,这里的值为空 printf("%d,%d", *(a + 1), *(p - 1)); //*(a+1) a+1取a的第一个元素的偏移一个int地址,则为a[1]的地址 *(a+1)为解引用,取这里地址存放的值则为2 //*(p-1) 则这里的值为a[4]的值,所以为5 return 0; }

最后输出的结果为2,5

猜你喜欢

转载自www.cnblogs.com/shenji/p/12505570.html