指针的引用 与 二级指针
指针引用:*&top 二级指针:**top
top指针可以用指针的引用,也可以用二级指针,都能达到修改top指针的指向的目的。
个人感觉指针的引用比二级指针更容易理解。
例子:
//指针引用小测试
#include <iostream>
using namespace std;
void swap(int *&top) //参数是 &top 参数类型是int*
{
int a = 8;
top = &a;
}
int main()
{
int *top = NULL;
swap(top);
cout<<*top<<endl;
return 0;
}
//2级指针小测试
#include <iostream>
using namespace std;
void swap(int **top) 参数是top ,类型是int*
{
int a = 8;
(*top) = &a;
}
int main()
{
int *top = NULL;
swap(&top);
cout<<*top<<endl;
return 0;
}
指针引用:*&top 二级指针:**top
top指针可以用指针的引用,也可以用二级指针,都能达到修改top指针的指向的目的。
指针引用:引用就是别名,两个本质上一样,所以直接操作top指针。
二级指针:主函数中需要传一级指针的地址进入子函数(如:&top)。
个人感觉指针的引用比二级指针更容易理解。
例子:
//指针引用小测试
#include <iostream>
using namespace std;
void swap(int *&top) //参数是 &top 参数类型是int*
{
int a = 8;
top = &a;
}
int main()
{
int *top = NULL;
swap(top);
cout<<*top<<endl;
return 0;
}
//2级指针小测试
#include <iostream>
using namespace std;
void swap(int **top) 参数是top ,类型是int*
{
int a = 8;
(*top) = &a;
}
int main()
{
int *top = NULL;
swap(&top);
cout<<*top<<endl;
return 0;
}