Meaning of *& and **& in C++

128,220

Solution 1

That is taking the parameter by reference. So in the first case you are taking a pointer parameter by reference so whatever modification you do to the value of the pointer is reflected outside the function. Second is the simlilar to first one with the only difference being that it is a double pointer. See this example:

void pass_by_value(int* p)
{
    //Allocate memory for int and store the address in p
    p = new int;
}

void pass_by_reference(int*& p)
{
    p = new int;
}

int main()
{
    int* p1 = NULL;
    int* p2 = NULL;

    pass_by_value(p1); //p1 will still be NULL after this call
    pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory

    return 0;
}

Solution 2

First is a reference to a pointer, second is a reference to a pointer to a pointer. See also FAQ on how pointers and references differ.

void foo(int*& x, int**& y) {
    // modifying x or y here will modify a or b in main
}

int main() {
    int val = 42;
    int *a  = &val;
    int **b = &a;

    foo(a, b);
    return 0;
}

Solution 3

That's passing a pointer by reference rather than by value. This for example allows altering the pointer (not the pointed-to object) in the function is such way that the calling code sees the change.

Compare:

void nochange( int* pointer ) //passed by value
{
   pointer++; // change will be discarded once function returns
}

void change( int*& pointer ) //passed by reference
{
   pointer++; // change will persist when function returns
}

Solution 4

An int* is a pointer to an int, so int*& must be a reference to a pointer to an int. Similarly, int** is a pointer to a pointer to an int, so int**& must be a reference to a pointer to a pointer to an int.

Solution 5

*& signifies the receiving the pointer by reference. It means it is an alias for the passing parameter. So, it affects the passing parameter.

#include <iostream>
using namespace std;

void foo(int *ptr)
{
    ptr = new int(50);    // Modifying the pointer to point to a different location
    cout << "In foo:\t" << *ptr << "\n"; 
    delete ptr ;
}

void bar(int *& ptr)
{
    ptr = new int(80);    // Modifying the pointer to point to a different location
    cout << "In bar:\t" << *ptr << "\n"; 
    // Deleting the pointer will result the actual passed parameter dangling
}
int main()
{
    int temp = 100 ;
    int *p = &temp ;

    cout << "Before foo:\t" << *p << "\n";
    foo(p) ;
    cout << "After foo:\t" << *p << "\n";

    cout << "Before bar:\t" << *p << "\n";
    bar(p) ;
    cout << "After bar:\t" << *p << "\n";

    delete p;

    return 0;
}

Output:

Before foo: 100
In foo: 50
After foo:  100
Before bar: 100
In bar: 80
After bar:  80
Share:
128,220

Related videos on Youtube

sdffadsf
Author by

sdffadsf

Updated on March 13, 2022

Comments

  • sdffadsf
    sdffadsf over 2 years

    I found these symbols in a function declaration several times, but I don't know what they mean.

    Example:

    void raccogli_dati(double **& V, double **p, int N) { 
      int ultimo = 3; 
      V = new double * [N/2]; 
      for(int i=0; i < N/2; i++) { 
        V[i] = new double[N/2], std :: clog << "digita " << N/2 - i
                     << " valori per la parte superiore della matrice V: "; 
        for(int j=i; j < N/2; j++) 
          std :: cin >> V[i][j], p[ultimo++][0] = (V[i][j] /= sqrt(p[i][0]*p[j][0]));
      } 
      for(int i=1; i < N/2; i++) 
        for(int j=0; j < i; j++) 
           V[i][j] = V[j][i];
    }
    
    • Mr. Shickadance
      Mr. Shickadance about 13 years
      In actual code or a book? To me it just looks like notation for a general function. One which returns a single pointer, and the other a pointer to a pointer.
    • Alexandre C.
      Alexandre C. about 13 years
      Those are references to pointers.
    • JW.ZG
      JW.ZG over 8 years
      This website may be helpful to you. )
  • corlettk
    corlettk about 13 years
    I know I shouldn't really, but I upvoted this answer simply because it contains an a-pointer-a-pointer-to-a-reference-to-an-allusion-to-the-mea‌​ning-of-life. Somehow I think we're a few years away from Deep Thought though.
  • Sheldon
    Sheldon about 7 years
    i understand their literal meanings, can u write an example to illustrate more effectively among them?
  • XavierStuvw
    XavierStuvw over 4 years
    +1 for the tip to read the declarations from right to left: this speeds up understanding greatly, and also explains why interpretations of C++ construct can be difficult sometimes (for those who normally read from left to right at least)