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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM