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

Started by
44 comments, last by visitor 14 years, 5 months ago
Hi mates! Why do you write "int *number" instead of "int* number"? Thanks.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
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.
int* x, y;


What is x, what is y?
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.
[size="2"]I like the Walrus best.
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.
visualnovelty.com - Novelty - Visual novel maker
I like my whitespace so I prefer to have those sigils floating in it:

int * a;
int & b;
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.
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.
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.

[size="2"]I like the Walrus best.
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.

This topic is closed to new replies.

Advertisement