Pointer to Pointers & Casting Pointers

I just started to learn C++ a few days ago and I find pointers to be quite challenging. Below is some code that I see used alot for pointers to classes in games(which get used to access player info within the class). Can anyone explain to me how the below actually works. I have never seen pointers written like this, even in the C++ tutorial here on this site. I understand the first line which is basically declaring the pointer, but what confuses me is the how the pointer is being deferenced, and the casting in the second line.
1
2
3
example code:
  cLocalPlayer *pLocalPlayer;
  pLocalPlayer = (cLocalPlayer**)(some address here eg. 0x045000)
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. I'm no expert, but here's how I analyze that line:

"Set pLocalPlayer equal to the given memory address and treat it as a pointer to another cLocalPlayer pointer".

As you know, a pointer is basically a variable which holds a memory address.
cLocalPlayer** is actually a pointer to a pointer (hence the double asterisk).
Remember that when the asterisk follows the typename, it is a pointer type, when it precedes a variable name, it is dereferencing.

So in your example, you are setting pLocalPlayer's value (a memory address) to the memory address given and you tell the compiler to treat it as a pointer to another cLocalPlayerPointer.

Sorry for the abstract explanation, but that's probably the best I could do without making a drawing.
Maybe this article can help you understand a little better:
http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer

I'm wondering if it is allowed to assign a pointer to a pointer to a regular pointer though - I don't think so.
I think your example should be:

1
2
cLocalPlayer **pLocalPlayer; //double asterisk here
pLocalPlayer = (cLocalPlayer**)(some address here eg. 0x045000)


Maybe one of the experts around here can chime in.

Hope that helps,
NwN
Thanks for the reply NwN. Just to clarify, cLocalPlayer is not a pointer but rather a class within a game that would hold things like health,ammo,etc...
And the pLocalPlayer is simply a pointer to the class which can be used later to access these values. The address of (pLocalPlayer) would be whatever is in the " (some address here eg.0x045000)". If that changes anything you mentioned above I would appreciate if you could follow up this message.
Last edited on
closed account (o3hC5Di1)
Hi there,

Sorry, I thought cLocalPlayer being a class was implied - I should have mentioned that.
It doesn't change what I said.

All the best,
NwN
From the sounds of what you are trying to do I'm not sure why you'd need a pointer to a pointer to implement it. I wouldn't overcomplicate things, especially if you've only been c++'ing for a few days.
Topic archived. No new replies allowed.