📜  将指针传递给指针与任何函数的指针地址之间的区别

📅  最后修改于: 2021-05-30 03:41:02             🧑  作者: Mango

在本文中,将“指针指向指针”和“指针地址”传递给函数之间的区别。在C或C++编程语言中,众所周知指针包含变量的地址或任何内存位置。如果指针指向内存位置,则可以使用它来更改变量的值。

对于函数,任何指针都可以自己传递,也可以通过指针的地址传递。但是,如果要在函数永久更改指针的内存位置,则必须使用函数内的引用或双指针捕获指针,即,假定在本地函数内创建了任何内存。

此后,它指向该函数捕获的指针,而没有任何引用或任何双指针。在这种情况下,主函数内部没有变化,因为它没有被函数定义内的引用或双指针捕获。

程序1:

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

说明:输出没有变化的原因是为函数创建了一个复制指针。因此,指针地址的更改不会在函数反映出来。引用或双指针可用于此目的。

程式2:

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()函数的参考或双指针被捕获。

程序3:

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++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”