int * & a

What does this mean " bool function1(int * & a) "
is it that address of a is a pointer to the integer that will be inputed in the function1 call? and why would this be used in general?
thank you
It's a reference to a pointer.

Reference examples:
1
2
3
4
int i = 10;
int &ri = i; // reference to i (a different name for i)

ri = 99; // i is now 99 


You pass arguments by reference if you want to change the actual parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void fa(int i)
{
    i = 99; // i is a local copy
}

void fb(int &i)
{
    i = 99; // i is a reference
}

int main()
{
    int number = 1;

    fa(number);
    std::clog << "after fa(): " << number << '\n';
    fb(number);
    std::clog<< "after fb(): " << number << '\n';
}


In your case, you pass a pointer by reference. The effect is the same as in the number example. The value of the pointer (a memory address: a number) can be changed in the function and the changes will be visible outside the function.

The confusion is because & is also used as the address-of operator, which gets the address of something.

Edit: typo.
Last edited on
Topic archived. No new replies allowed.