深入理解c++指针的指针和指针的引用

2020-01-06 15:19:23王振洲

•**p:两次解引用是指向main()方法里*pn的内容

指针的引用

再看一下指针的引用代码


int m_value = 1;

void func(int *&p)
{
  p = &m_value;

  // 也可以根据你的需求分配内存
  p = new int;
  *p = 5;
}

int main(int argc, char *argv[])
{
  int n = 2;
  int *pn = &n;
  cout << *pn << endl;
  func(pn);
  cout << *pn <<endl;
  return 0;
}

c++,指针

看一下func(int *&p)方法

•p: 是指针的引用,main()方法里的 *pn

•*p:是main()方法里的pn指向的内容。

以上这篇深入理解c++指针的指针和指针的引用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持ASPKU。



注:相关教程知识阅读请移步到C++教程频道。