在本文中,将“指向指针的指针”和“指向指针的地址”传递给函数的区别。在 C 或 C++ 编程语言中,众所周知,指针保存变量的地址或任何内存位置。如果指针指向内存位置,则可以使用它来更改变量的值。
对于函数,任何指针都可以通过自身传递,也可以通过指针的地址传递。但是如果指针的内存位置要在函数永久更改,则必须使用函数内的引用或双指针捕获它,即假设在本地函数内创建了任何内存。
之后,它指向函数捕获的指针,没有任何引用或任何双指针。在这种情况下,主函数内部没有变化,因为它没有被函数定义内部的引用或双指针捕获。
方案一:
C++
// C++ program to illustrate the
// concepts pointers
#include
#include
using namespace std;
// Function to assign the value of
// pointer ptr to another location
void foo(int* ptr)
{
int b = 2;
// ptr= (int*)malloc(sizeof(int));
// It can be executed instead of
// the ptr=&b but nothing change
ptr = &b;
}
// Driver Code
int main()
{
int a = 5;
int* ptr = &a;
cout << "Before the function ptr: "
<< *ptr << endl;
foo(ptr);
cout << "After the function ptr: "
<< *ptr << endl;
return 0;
}
C++
// C++ program to illustrate the
// concepts of pointer
#include
#include
using namespace std;
// Function to change the pointer ptr
void foo(int* ptr)
{
int b = 2;
// The change will only be for
// the new value of the ptr
*ptr = b;
}
// Driver Code
int main()
{
int a = 5;
int* ptr = &a;
cout << "Before the function, "
<< "new value of the ptr: "
<< *ptr << endl;
// Function Call
foo(ptr);
cout << "After the function, "
<< "new value of the ptr: "
<< *ptr << endl;
return 0;
}
C++
// C++ program to illustrate the
// concepts of pointer
#include
#include
using namespace std;
// Function to change the value of
// the pointers ptr1
void foo(int** ptr1)
{
int b = 2;
// ptr = &b;
// For this assignment, the
// captured value will be like
// void foo(int* &ptr)
*ptr1 = &b;
}
// Driver Code
int main()
{
int a = 5;
int* ptr = &a;
cout << "Before the function, "
<< "new value of the ptr: "
<< *ptr << endl;
// Function Call
foo(&ptr);
cout << "After the function, "
<< "new value of the ptr: "
<< *ptr << endl;
return 0;
}
输出:
Before the function ptr: 5
After the function ptr: 5
解释:输出没有变化的原因是它为函数创建了一个复制指针。因此,指针地址的变化不会反映在函数。为此可以使用引用或双指针。
方案二:
C++
// C++ program to illustrate the
// concepts of pointer
#include
#include
using namespace std;
// Function to change the pointer ptr
void foo(int* ptr)
{
int b = 2;
// The change will only be for
// the new value of the ptr
*ptr = b;
}
// Driver Code
int main()
{
int a = 5;
int* ptr = &a;
cout << "Before the function, "
<< "new value of the ptr: "
<< *ptr << endl;
// Function Call
foo(ptr);
cout << "After the function, "
<< "new value of the ptr: "
<< *ptr << endl;
return 0;
}
输出:
Before the function, new value of the ptr: 5
After the function, new value of the ptr: 2
解释:
如果想替换存储在PTR存储器把foo()函数完成时,PTR必须与FOO()函数的参考或双指针被捕获。
方案三:
C++
// C++ program to illustrate the
// concepts of pointer
#include
#include
using namespace std;
// Function to change the value of
// the pointers ptr1
void foo(int** ptr1)
{
int b = 2;
// ptr = &b;
// For this assignment, the
// captured value will be like
// void foo(int* &ptr)
*ptr1 = &b;
}
// Driver Code
int main()
{
int a = 5;
int* ptr = &a;
cout << "Before the function, "
<< "new value of the ptr: "
<< *ptr << endl;
// Function Call
foo(&ptr);
cout << "After the function, "
<< "new value of the ptr: "
<< *ptr << endl;
return 0;
}
输出:
Before the function, new value of the ptr: 5
After the function, new value of the ptr: 2
解释:
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。