• Advertisement
Sign in to follow this  

Pointer to pointers

This topic is 4795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using a pointer to a pointer ( myStruct** ) and the compiler is giving me some trouble: Say you have myStruct* pointer = NULL; myStruct** pointer2 = &pointer; say I want to set pointer to a through pointer2, but the vc++ compiler says it's not right: pointer2* = a; and what if you want to get to a member? pointer2*->myMember (); // Doesn't work... any help?

Share this post


Link to post
Share on other sites
Advertisement
Your syntax is a little wrong. It should be:


*pointer2 = a;

(*pointer2)->myMember ();

Share this post


Link to post
Share on other sites
Quote:
Original post by grekster
shouldnt it be

*pointer2 = &a;

??


Only if a is declared as a struct:

myStruct a;

If a is declared as a pointer to a myStruct, no problem:

myStruct* a;




Share this post


Link to post
Share on other sites
The point is, the * comes before the variable, otherwise it's seen as multiplication.

I find it better to put the * touching the variable, even in the declaration. Because it you declare more than one pointer you have to put a star in front of each one:
myStruct *pointer1, *pointer2;
rather than
myStruct* pointer1, pointer2;
which is incorrect. (pointer2 would not be a pointer it would be an actual struct!)
Either that or do a typedef and be rid of the *'s on the declaration line.

Just wait until you've got to maintain someone's code where they used three stars...
int SomeFunc(SomeType ***SomeVar)

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
The point is, the * comes before the variable, otherwise it's seen as multiplication.

I find it better to put the * touching the variable, even in the declaration. Because it you declare more than one pointer you have to put a star in front of each one:
myStruct *pointer1, *pointer2;
rather than
myStruct* pointer1, pointer2;
which is incorrect. (pointer2 would not be a pointer it would be an actual struct!)


Rather unfortunate behaviour of the language(s; both C and C++) IMHO; declaring differently typed variables on the same line in a statically typed language is quite unintuitive, as is sticking part of the type information on the variable name instead of the type name.

P.S. I was trying to find an article where Stroustrup was talking about this... I couldn't find it, but during my search I ran across this java to C++ tutorial from Brown's CS department. It saddens me deeply to think that such a document - dated to 2004, even - is thinking in terms of printf, char *'s and stdio.h. (Oh, and those stupid m_ prefixes too.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
those stupid m_ prefixes


*shudder*

as to the asterisk problem, i usually put it with the type name, not the variable name. for instance:

Thing* foo;

is a "Thing pointer", and i need to read it that way. It seems weird when compared to:

Thing *foo;

The "pointer" part of it has to do with the type, not the variable name, so that's where i put it. The multiple declarations problem is legit, but that's why 99% of the time, i put each one on it's own line.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement