-2

What is different between two fucntions bellow?

void init1(int **&a, int n)
{
    a = new int *[n];
    for (int i = 0; i<n; i++)
        a[i] = new int [n];
}

and

void init1(int **a, int n)
{
    a = new int *[n];
    for (int i = 0; i<n; i++)
        a[i] = new int [n];
}

Thanks,

Joey
  • 135
  • 5

2 Answers2

0

The difference between the two is that in the int **&a version, a has been passed by reference. If you change the value of the pointer itself (that is to say, write code like a = a+5;) the value of a will be modified in the original calling function.

Xirema
  • 18,577
  • 4
  • 26
  • 60
0

The first function passes the double pointer a by reference and the second function just passes the double pointer a as an argument.

In the first function, any modification done to a will persist after the function has exited unlike in the second where any modification to a will not persist.

NickLamp
  • 782
  • 4
  • 10