Pointers in cpp

I saw a pointer which was declared as *& in a program... what does *& represent?
It's a reference to a pointer.
Well lets see...

the "*" is the de-reference operator, and the "&" is the reference operator.

So,although i have never seen this before, it seems that the code above is de-referencing the reference of the object, or returning the memory location of the object.
closed account (zb0S216C)
@pogrady: You cannot dereference a reference.

As Caligulaminus said, it's a reference to a pointer. Some, if not most, programmers will say something similar to: "it's pointless!", when in fact, it's not. For example: Say I wanted to move a given pointer from one location to another. You'd think of something like this:

1
2
3
4
void move_pointer(int *pointer)
{
    pointer = some_address;
}

This is wrong, because pointer is a copy of the pointer passed to it. The C++ solution is to use a reference to a pointer:

1
2
3
4
void move_pointer(int *&pointer)
{
    pointer = some_address;
}

Because we're referring to the pointer given to pointer, we can modify the original pointer. We could use a pointer to a pointer, but this is C++.

Wazzak
Last edited on
Topic archived. No new replies allowed.