C指针: 指向变量的地址,想象成房间号
&: 取地址符号
*:间接访问符号, 访问p所存地址的内容
#include <iostream> int main(int argc, const char * argv[]) { int m = 5; int *p = &m; std::cout << "p = " << p << std::endl; //0x7ffeefbff5bc std::cout << "*p = " << *p << std::endl; //5 return 0; }
#include <iostream> int main(int argc, const char * argv[]) { int m = 5; int *p = &m; std::cout << "p = " << p << std::endl; //0x7ffeefbff5bc std::cout << "*p = " << *p << std::endl; //5 std::cout << "&p = " << &p << std::endl;//0x7ffeefbff5a0 int **pp = &p; //& 不可以使用两次 int b = **pp; std::cout << "b = " << b << std::endl; //5 std::cout << "*pp = " << *pp << std::endl;//0x7ffeefbff58c std::cout << "pp = " << pp << std::endl;//0x7ffeefbff570 return 0; }
另外注意指针要初始化,否则成为野指针。
参考 https://www.cnblogs.com/mymindview/p/5247485.html