C++指针变量的定义和使用

2020-01-06 12:19:03于海丽

{
   int i=3;
   int *iptr=&i;
   int **iptrptr=&iptr;//iptr也是变量,也能够获取它的地址
   cout <<"Address of Var i=" <<iptr <<endl;//输出iptr存储的内容,即i在内存中的地址
   cout <<"Data of Var i=" <<*iptr <<endl;//输出iptr所指向的变量
   cout <<"Address of Pointer iptr=" <<iptrptr <<endl;//输出iptr在内存中的地址
   cout <<"Address of Var i=" <<*iptrptr <<endl;//输出iptrptr所指向的变量,即iptr
   *iptr=2+*iptr;//*iptr可以作左值
   cout <<"Data of Var i=" <<*iptr <<endl;
   return 0;
}

运行结果:
Address of Var i=0x0012FF7C
Data of Var i=3
Address of Pointer iptr=0x0012FF78