Double Pointer Trouble

closed account (zb0S216C)
I'm trying to fully understand the pointer concept. I'm moving onto the double pointer( pointer to a pointer ). Here's the code:
1
2
3
4
5
6
7
8
9
struct BASE
{
} Base, *p_Base( &Base ), **pp_Base( &p_Base );

int main( )
{
    pp_Base->Member = 0;
    return 0;
}

Error: request for member 'Member' in '*p_Base', which is of non-class type 'BASE*'

Any thoughts on why I'm receiving this error? The line that's causing the compilation error is highlighted.

Any help is appreciated, as always.
The -> only deferences the pointer once, so you end up trying to access a member of a pointer. Since a pointer is not a class, that is illegal. If it helps, you can conceptually split it up:

1
2
3
//pp_Base->Member == (*pp_Base).Member
Base* ptr_to_Base = (*pp_Base);
ptr_to_Base.Member; //<-- Error, can't use the dot operator on a pointer like this, need to use -> 


Thus, the correct way to do it would be to do this:

1
2
3
(*pp_Base)->Member = 0;
//dereference the pointer to pointer to get just a Base*, then use -> to deference it again
// and access Member 
The -> operator is shorthand for (*).

So pp_Base->Member is the same as (*pp_Base).Member

With that in mind.. since pp_Base is a pointer to a pointer, that means *pp_base is a pointer (not a BASE). Therefore you can't do .Member on it because pointers don't have any members.

The solution here would be to either dereference the pointer twice:

(**pp_Base).Member = 0;

or dereference it once and use the -> operator to deference it the 2nd time:

(*pp_Base)->Member = 0;


EDIT: I'm too slow for firedraco

YOU CAN TOUCH THIS!!!!!1111 /EDIT
Last edited on
closed account (zb0S216C)
You guys are awesome. I just added ( *pp_Base ) to the front of pp_Base->Member and it works. Thanks, much appreciated :)
Topic archived. No new replies allowed.