C++中指针的传递

先决条件 : C++中指针与引用的关系 .

null

为了清楚地理解,让我们比较一下“指针到指针”和“指针引用”在某些情况下的用法。

注: 在C和C++中都允许使用“指针指向”,但是我们只能在C++中使用“引用指针”。

向函数传递指针

如果指针作为参数传递给函数并试图修改,那么对指针所做的更改不会反映到该函数之外。这是因为只有指针的一个副本被传递给函数。可以说,“通过指针传递”是 通过值传递指针 .在大多数情况下,这并不构成问题。但当你修改函数中的指针时,问题就来了。不是修改变量,而是只修改指针的副本,而原始指针保持不变。

下面的程序说明了这一点:

CPP

#include <iostream>
using namespace std;
int global_Var = 42;
// function to change pointer value
void changePointerValue( int * pp)
{
pp = &global_Var;
}
int main()
{
int var = 23;
int * ptr_to_var = &var;
cout << "Passing Pointer to function:" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
changePointerValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 23
return 0;
}


输出:

Passing Pointer to function:
Before :23
After :23

将“指针指向指针”作为参数传递给函数

通过将指针的地址传递给函数而不是实际函数的副本,可以解决上述问题。为此,函数参数应接受“指针指向指针”,如下程序所示:

CPP

#include <iostream>
using namespace std;
int global_var = 42;
// function to change pointer to pointer value
void changePointerValue( int ** ptr_ptr)
{
*ptr_ptr = &global_var;
}
int main()
{
int var = 23;
int * pointer_to_var = &var;
cout << "Passing a pointer to a pointer to function " << endl;
cout << "Before :" << *pointer_to_var << endl; // display 23
changePointerValue(&pointer_to_var);
cout << "After :" << *pointer_to_var << endl; // display 42
return 0;
}


输出:

Passing a pointer to a pointer to function 
Before :23
After :42

如何使用“Reference to pointer”参数调用函数?

引用允许被调用函数修改调用函数的局部变量。例如,考虑下面的示例程序,其中FUNE()能够修改MIN()的局部变量x。

CPP

#include<iostream>
using namespace std;
void fun( int &x) {
x = 20;
}
int main() {
int x = 10;
fun(x);
cout<< "New value of x is " <<x;
return 0;
}


输出:

New value of x is 20

下面的程序显示了如何将“指针引用”传递给函数:

CPP

#include <iostream>
using namespace std;
int gobal_var = 42;
// function to change Reference to pointer value
void changeReferenceValue( int *& pp)
{
pp = &gobal_var;
}
int main()
{
int var = 23;
int * ptr_to_var = &var;
cout << "Passing a Reference to a pointer to function" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
changeReferenceValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}


输出:

Passing a Reference to a pointer to function
Before :23
After :42

从函数返回指针

CPP

#include <iostream>
using namespace std;
int global_var = 42;
// function to return a pointer
int * returnPointerValue()
{
return &global_var;
}
int main()
{
int var = 23;
int * ptr_to_var = &var;
cout << "Return a pointer from a function " << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
ptr_to_var = returnPointerValue();
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}


输出:

Return a pointer from a function 
Before :23
After :42

从函数返回引用

CPP

#include <iostream>
using namespace std;
int global_var = 42;
// function to return reference value
int & ReturnReference()
{
return global_var;
}
int main()
{
int var = 23;
int * ptr_to_var = &var;
cout << "Returning a Reference " << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
ptr_to_var = &ReturnReference();
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}


输出:

Returning a Reference 
Before :23
After :42

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享