Pointer declarations ...

Started by
12 comments, last by rip-off 18 years, 7 months ago
I've always wondered which is considered more 'correct' ...

int *pointer;
int* pointer;
The first one is the way I learned it, and the way I see most use it. However, the second way, really does make more sense when you think about it. How many people use the first method? How many use the second method? Anyone ever worked at a software company which enforced a certain format? I'm always curious about different formatting people use when programming, just though I'd see what people think of this.
Advertisement
This gets asked a lot.

See Bjarne's FAQ:

Quote: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.
I use
type * pointer_to_type;

because I can read it the best. But I have bad eyes anyway ... nice link petewood.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
I use :

int* myIntPtr;

ie. the 'Ptr' suffix so I know what it is later on in my code.

Mr. Creamy.

I recently heard an interesting argument for using the int * pointer method. Neither the int *pointer or int* pointer methods generalize well when you start adding const or volatile qualifiers to the pointer type and/or the pointed to type, especially when you get multiple layers of indirection. e.g:

int const * volatile pointer;

or even:

int * const * const pointer;
I use, and always have used:
int *ptr;

Just a matter of style and avoiding mistakes when I was younger:
int* ptr1, ptr2; // looks like both would be pointers, where they aren't
int *ptr1, *ptr2; // both pointers; what you would (presumably) want

That's just how I do it, though.
foo *var;

And I do know of companies/courses that have required one or the other as part of their standardized format.
it was

type * var;

for me but lately i use

type *var;

becaes it is handy in multiple declations

as a theroethical point -

how is a reference to a pointer declared?

int & * p;
int * & p;

i'll probably never need to know... but they *both* look wrong to me


A reference to pointer is int * &
Quote:Original post by SiCrane
I recently heard an interesting argument for using the int * pointer method. Neither the int *pointer or int* pointer methods generalize well when you start adding const or volatile qualifiers to the pointer type and/or the pointed to type, especially when you get multiple layers of indirection. e.g:

int const * volatile pointer;

or even:

int * const * const pointer;

Or mutable. Or auto. Or register. [grin] There are loads of interesting but mostly useless keywords (well, mutable ins't useless, it's just not very useful) that don't go well with int* pointer and int *pointer..

For the record, I put the asterisk inbetween, with a space on either side.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!

This topic is closed to new replies.

Advertisement