Location of the * in pointers

Started by
15 comments, last by dmatter 15 years, 9 months ago
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?
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;
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.
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 pointersptr<const Foo> y; // const works tooptr<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.
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]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Take a look at this.

http://en.wikipedia.org/wiki/Declaration_reflects_use

I still prefer it this way, though:

int* f;

and not:

int *f;

But just my opinion.
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]
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?
"Three Star Programmer"
now that's something only a programmer would find funny-LOL!
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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.

This topic is closed to new replies.

Advertisement