Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does *& mean in a function parameter

If I have a function that takes int *&, what does it means? How can I pass just an int or a pointer int to that function?

function(int *& mynumber);

Whenever I try to pass a pointer to that function it says:

error: no matching function for call to 'function(int *)'
note: candidate is 'function(int *&)'
like image 1000
DogDog Avatar asked Nov 15 '10 15:11

DogDog


1 Answers

It's a reference to a pointer to an int. This means the function in question can modify the pointer as well as the int itself.

You can just pass a pointer in, the one complication being that the pointer needs to be an l-value, not just an r-value, so for example

int myint;
function(&myint);

alone isn't sufficient and neither would 0/NULL be allowable, Where as:

int myint;
int *myintptr = &myint;
function(myintptr);

would be acceptable. When the function returns it's quite possible that myintptr would no longer point to what it was initially pointing to.

int *myintptr = NULL;
function(myintptr);

might also make sense if the function was expecting to allocate the memory when given a NULL pointer. Check the documentation provided with the function (or read the source!) to see how the pointer is expected to be used.

like image 183
Flexo Avatar answered Oct 10 '22 01:10

Flexo