Jump to content
  • Advertisement
Sign in to follow this  
DaedalusOwnsYou

Location of the * in pointers

This topic is 3644 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 often see pointers in c++ written both as
type* x;
and
type *x;
There is no difference because the * is a separate token, but why is the * not treated as part of the type? I'm not sure how to ask this because it seems like a really stupid question, it just bugs me. It seems like the * should be part of the type, not an extra flag. Why does the * get special treatment when declaring pointers to variables?

Share this post


Link to post
Share on other sites
Advertisement
It depends on how you chose to interpret it...

1) x is of type: type*

2) x is a pointer to an instance of type: type

C++ programmers typically prefer the first interpretation, whereas C programmers prefer the latter.

Personally I have whitespace around both sides of the asterix: type * x;

Share this post


Link to post
Share on other sites
This issue has always bothered me a bit too. D from Digital Mars "fixes" this issue. However, separating the * from the type declaration does offer some flexibility.

int a, *b = NULL, *c, d = 0; // a is int, b is int pointer, c is int pointer, d is int.
float* e, f; // Whoops! f looks like it would be a pointer, but it's not!

All on one line, pointers and non-pointers (of the same type) can be declared (and initialized). This can be sorta nice, but the second line illustrates a simple pitfall; both e and f kinda look like they should be pointers. When I first learned C/C++, I thought they were. Now I know better. :-)

In all of my code, I "pair" the * with the type and pretend C++ honors that (except, of course, when I can't, perhaps when doing something like the above code with the ints). I usually do this:

void foo(int* bar) { ... }

The rational behind why C/C++ works this way, I have no idea.

Share this post


Link to post
Share on other sites
AFAICT, C++0x will make possible an alternative form, which in my opinion is infinitely more elegant and readable:
template<typename T> using ptr = T*;

This enables the following syntax:

ptr<Foo> x, x2; // both are pointers
ptr<const Foo> y; // const works too
ptr<ptr<const Foo>> z; // and no weird rules about where the const goes

It's much more in line with how types are supposed to work in C++, as opposed to how they have to work thanks to C.

Share this post


Link to post
Share on other sites
Yeah it's bugged me too since I first saw it and could couldn't settle on either so I just ended up putting a space on both sides even though it's extra work on the fingers.
You aren't the only one though since it's even mentioned on Stroustroup's FAQ here:

"Is ``int* p;'' right or is ``int *p;'' right?
Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say ``int*p;'' or ``int * p;''

The choice between ``int* p;'' and ``int *p;'' is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

A ``typical C programmer'' writes ``int *p;'' and explains it ``*p is what is the int'' emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A ``typical C++ programmer'' writes ``int* p;'' and explains it ``p is a pointer to an int'' emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int* p, p1; // probable error: p1 is not an int*

Placing the * closer to the name does not make this kind of error significantly less likely.

int *p, p1; // probable error?

Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:

int* p = &i;
int p1 = p; // error: int initialized by int*

And if they do, the compiler will complain.

Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears. See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.
"

Just pick a style and go with it!
I think basically the only thing anyone agrees on is to pick a style and be consistent.
I know I've come across some books where the author switches from int* p to int *p every other page and it bugs me to no end!


[Edited by - daviangel on July 1, 2008 7:35:19 PM]

Share this post


Link to post
Share on other sites
I prefer type *var as it basically says, "var is a pointer to an instance type." I can't see there being a "pointer" type (which something like type* var implies), but I can see there being a type, and a variable that's a pointer to an instance of that type.

[Edited by - agi_shi on July 1, 2008 7:48:48 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
I prefer type *var as it basically says, "var is a pointer to a type." I can't see there being a "pointer" type (which something like type* var implies), but I can see there being a type, and a variable that's a pointer to that type.

How could anything point to a type?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by agi_shi
var is a pointer to a type.

How could anything point to a type?

Note how "var" and "type" are in a fixed font. He was making a generic argument.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!