Jump to content
  • Advertisement
Sign in to follow this  
riruilo

[c++] Why do you write "int *number" instead of "int* number"?

This topic is 3170 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

Advertisement
Its a personal choice, and not a very important one. I used to write "int * number", because I like whitespace. But for quite a while now I just attach the asterisk to the variable name. No particular reason.

That is, when I use raw pointers at all, which I try to minimise.

Share this post


Link to post
Share on other sites
Some people (like I) prefer to place the * next to the type to enforce the idea of the pointer being of that type.

int* VariableName.

Share this post


Link to post
Share on other sites
I put the asterisk next to the type (int* p) because I think and read it like "p is an int-pointer (int*)". Some will put it next to the variable instead (int *p) and read it like "p is a pointer(*) to an int".

I've found it's easier to be consistent when putting the asterisk next to the type and treating it as a single type, for instance:
Foo* p = reinterpret_cast<Foo*>( pOther );


But there's no right or wrong. It comes down to individual preference.

Antheus: I would never want to write ambiguous code like that. Here's how I would declare two subsequent pointers of the same type:
int* x;
int* y;

Furthermore I wouldn't declare pointers without directly initializing them i.e. have them point at something in memory, or NULL.

The most common case for me is to use pointers as arguments in function headers, where the position of the asterisk makes no significant difference.

Share this post


Link to post
Share on other sites
I think it is better to write the * near the name because it is more easelly to spot weather the var name is a pointer or not;

take the following example:

type* a,*b;

you may be tempted to believe that a and b are of different types when you fly with your eyes on the code.

It is still a personal choice.

Raxvan.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
int* x, y;


What is x, what is y?


x is a pointer to an int.
y is an int.

That is why the asterix should go with the identifier (in this case), so that people don't make the mistake of thinking they're both pointers, like:

int *x, y;

In other cases, if they're seperated:

int *x;
int y;

it doesn't matter so much, but i think 'type *identifier' is a good habit to have.

Share this post


Link to post
Share on other sites
Quote:
Original post by Makaan
I think it is better to write the * near the name because it is more easelly to spot weather the var name is a pointer or not;

take the following example:

type* a,*b;

you may be tempted to believe that a and b are of different types when you fly with your eyes on the code.

It is still a personal choice.

Raxvan.


I agree that's personal choice. I try though not to define many variables in one line for clarity's sake.

Share this post


Link to post
Share on other sites
I do prefer int* x to int *x as I like the idea of having the type on one side, but technically the grammar of C/C++ isn't like that. Consider for instance int* x, y, which means one pointer to an int and one int are declared...

At the end of the day int * x would probably win in the consistency category. Especially when you start applying qualifiers like int*const* x where the no space version will look ugly regardless...

It boils down to preference, this is one silly thing that can trigger edit wars in code among people who believe their version is better.

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!