C++ 指针的访问
作者:互联网
1.访问指针
#include <iostream> #include <windows.h> #include <string> using namespace std; int main() { int room = 2; int room1 = 3; int* p1 = &room; int* p2 = p1; int* p3 = p1; printf("room的地址:%d\n", &room); printf("p1的值:%d p2的值:%d\n", p1, p2); printf("p3的值:%d\n", p3); p3 = &room1; printf("p3的值:%d,room1的地址:%d\n",p3,&room1);//不建议用这种方式 //使用16进制打印,把地址值当成一个无符号数来处理 printf("p1=0x%p\n",p1); printf("p1=0x%x\n", p1); printf("p1=0x%X\n", p1); system("pause"); return 0; }
2.访问指针的内容
#include <iostream> #include <windows.h> #include <string> using namespace std; int main() { int room = 2; int* girl = &room; int x = 0; x = *girl; //*是一个特殊的运算符,* girl 表示读取指针 girl 所指向 //的变量的值, *girl 相当于 room printf("x:%d\n",x); *girl = 4;//相当于room = 4; printf("room:%d,*girl:%d\n",room,*girl); system("pause"); return 0; }
标签:p1,room,int,C++,访问,printf,girl,include,指针 来源: https://www.cnblogs.com/smartlearn/p/16676203.html