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

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

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;
}

What does `*&` in a function declaration mean?

The & symbol in a C++ variable declaration means it's a reference.

It happens to be a reference to a pointer, which explains the semantics you're seeing; the called function can change the pointer in the calling context, since it has a reference to it.

So, to reiterate, the "operative symbol" here is not *&, that combination in itself doesn't mean a whole lot. The * is part of the type myStruct *, i.e. "pointer to myStruct", and the & makes it a reference, so you'd read it as "out is a reference to a pointer to myStruct".

The original programmer could have helped, in my opinion, by writing it as:

void myFunc(myStruct * &out)

or even (not my personal style, but of course still valid):

void myFunc(myStruct* &out)

Of course, there are many other opinions about style. :)

C++ Double Address Operator? (&&)

This is C++11 code. In C++11, the && token can be used to mean an "rvalue reference".

What does this mean const int*& var?

It is a reference to a pointer to an int that is const.

There is another post somewhat related, actually, here. My answer gives a sorta of general algorithm to figuring these things out.

This: const int& *var has no meaning, because you cannot have a pointer to reference.

If the const's and pointers are getting in the way, remember you can typedef these things:

typedef int* IntPointer;
typedef const IntPointer ConstIntPointer;

void foo(ConstIntPointer&); // pass by reference
void bar(const ConstIntPointer&); // pass by const reference
void baz(ConstIntPointer); // pass by value

Might make it easier to read.


If you need more help on C++, read this. More specifically, references.

References as variables do not take space:

int i; // takes sizeof(int)
int*pi = &i; // takes sizeof(int*)

int& ri = i; // takes no space.
// any operations done to ri
// are simply done to i

References as parameters use pointers to achieve the end effect:

void foo(int& i)
{
i = 12;
}

void foo_transformed(int *i)
{
*i = 12;
}

int main()
{
int i;

foo(i); // same as:
foo_transformed(&i); // to the compiler (only sort of)
}

So it's actually passing the address of i on the stack, so takes sizeof(int*) space on the stack. But don't start thinking about references as pointers. They are not the same.

What does this mean const int*& var?

It is a reference to a pointer to an int that is const.

There is another post somewhat related, actually, here. My answer gives a sorta of general algorithm to figuring these things out.

This: const int& *var has no meaning, because you cannot have a pointer to reference.

If the const's and pointers are getting in the way, remember you can typedef these things:

typedef int* IntPointer;
typedef const IntPointer ConstIntPointer;

void foo(ConstIntPointer&); // pass by reference
void bar(const ConstIntPointer&); // pass by const reference
void baz(ConstIntPointer); // pass by value

Might make it easier to read.


If you need more help on C++, read this. More specifically, references.

References as variables do not take space:

int i; // takes sizeof(int)
int*pi = &i; // takes sizeof(int*)

int& ri = i; // takes no space.
// any operations done to ri
// are simply done to i

References as parameters use pointers to achieve the end effect:

void foo(int& i)
{
i = 12;
}

void foo_transformed(int *i)
{
*i = 12;
}

int main()
{
int i;

foo(i); // same as:
foo_transformed(&i); // to the compiler (only sort of)
}

So it's actually passing the address of i on the stack, so takes sizeof(int*) space on the stack. But don't start thinking about references as pointers. They are not the same.

What does (int*) &var mean?

&i : means to take the address of i (which is a char*)

(int*)&i : casts that pointer to be a pointer to integer (which is bad/wrong to do, but you told the compiler to do it so it won't even give a warning)

int* p = (int*)&i; : a statement that says to store the pointer of i in p (and cast it too: the compiler won't even complain)

*p = 1234567892; : write this value, which is several bytes to the base location pointed to by p (which although p thinks it points to an int, is to char!). One of those bytes will end up in i, but the others will over write the bytes neighboring i.



Related Topics



Leave a reply



Submit