这里要用到二级指针,或者是引用,不然值不能传回去!
要是直接用
void test(int* n);
那么子函数得到的是*n的副本,在函数内n指向的内同改变,但是这个值传不回主函数!
#include <iostream>
void test(int** n);
int main()
{
int *n;
test(&n);
system("pause");
return 0;
}
void test(int** n)
{
int* a = new int;
*a = 5 ;
*n = a;
}
本文介绍了一种通过二级指针或引用的方式在C++中实现跨函数间值的有效传递方法。具体展示了如何利用二级指针使子函数能够修改主函数中的指针变量,并将修改后的值成功返回。
2195

被折叠的 条评论
为什么被折叠?



