Jump to content
  • Advertisement
Sign in to follow this  
AsOne

Pointer declarations ...

This topic is 4765 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'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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I use
type * pointer_to_type;

because I can read it the best. But I have bad eyes anyway ... nice link petewood.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I use :

int* myIntPtr;

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

Mr. Creamy.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
foo *var;

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

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!