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

Started by
44 comments, last by visitor 14 years, 5 months ago
Quote:Original post by supamike
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.


I disagree with you.

int is a type (integer)
int* is another type (a pointer to an integer)
what is "int space"? (or int * number)

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
Quote:Original post by Beyond_Repair
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.


I agree with you.

But all books/websites/samples usually use int *number.

BTW, thanks a lot for replies, I thought I was the only one using int*

I guess in a team, int * number is the best one, as you said, just to avoid team problems.
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.
it all creates the same assembly...

if you're in a team, you probably have to keep to team coding guidelines.
if you're on your own, who cares?
std::vector<int> numbers;boost::shared_ptr<int> number(new int(0));void foo(int &number) {  number = number * number;}std::auto_ptr<int> bar() {  return std::auto_ptr<int> (new int(0));}


What is this '*' symbol you keep using when declaring a variable? You can't multiply types by identifiers! :)

edit: on a serious note, I usually put the '*' by the variable name as you can see in my foo function when I put the '&' by the variable name.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by ricardo_ruiz_lopez
what is "int space"? (or int * number)

What is "unsigned int"? (Yes, it is its own type)

Now let's discuss tabs vs. spaces.
Quote:Original post by ricardo_ruiz_lopez
Quote:Original post by supamike
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.


I disagree with you.

int is a type (integer)
int* is another type (a pointer to an integer)
what is "int space"? (or int * number)


You can disagree if you want but the C++ standards for:

int* x,y;

only gives you one pointer (x is a pointer, y is not).

The rules about how that markup works means that the * is a modifer of the variable, not of the type.

So it seems much more appropriate to put the * next to the var name.

int *x,*y; //the right way to declare two int pointers

That's what i think but of course, people normally put it next to the type for some reason i can't fathom. Maybe it's the "wish" that the * was a modifier of the type, not the variable.

It definitely seems like that SHOULD be the behavior, but it isn't :P
Quote:Original post by Atrix256
You can disagree if you want but the C++ standards for:

int* x,y;

only gives you one pointer (x is a pointer, y is not).

The rules about how that markup works means that the * is a modifer of the variable, not of the type.

So it seems much more appropriate to put the * next to the var name.

int *x,*y; //the right way to declare two int pointers

That's what i think but of course, people normally put it next to the type for some reason i can't fathom. Maybe it's the "wish" that the * was a modifier of the type, not the variable.


You can also construct an example that shows that the "*" clearly belongs to the type, not the variable:
typedef int   INT;typedef int* PINT;PINT x, y;
IMO, seeing a "pointer to int" is a distinct type to "int", it really belongs closer to the type, even if that can cause you to make an occasional mistake when using comma (an error which you'll get told about at compile time anyway!).
//the right way to declare two int pointers:int* x;int* y; 
Quote:Original post by Beyond_Repair
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...


That's the very reason why I use a space before and after "*".

Quote:Original post by ricardo_ruiz_lopez
Quote:Original post by supamike
[...]
it doesn't matter so much, but i think 'type *identifier' is a good habit to have.


I disagree with you.

int is a type (integer)
int* is another type (a pointer to an integer)
what is "int space"? (or int * number)
I disagree with your disagreement :P

The language applies the * qualifier to the variable, not the type.

By pretending that int* is a type on its own, you create a model of your programming language in your mind that diverges from how things actually are.

Same goes for const. Will this compile?
  const int *i = 0, *j = 0;  j = i;
The answer is yes, because the type is not const int or const int *, but int. So i is const and j isn't.

If you instead read "int" as saying "the following variables are variants of the type int", it becomes clearer.
  int *const i = 0, *const j = 0;  j = i;
Of course you can decide to completely avoid multiple variable declarations in a single statement. In which case it is all a matter of personal preference.

I prefer to have the code look the way the language will interpret it as much as possible. Though I do wish that they had designed it the way you seem to want it with qualifiers belonging to the type instead of the variable.
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Quote:Original post by Cygon
Same goes for const. Will this compile?
  const int *i = 0, *j = 0;  j = i;
The answer is yes, because the type is not const int or const int *, but int. So i is const and j isn't.


No, both i and j are non-const pointers to const int. That means you can still assign other addresses to i and j, but you can't modify the pointee.

If you want i and j to be const pointers to whatever cv-qualified entites, you have to put the const qualifier on the right:

int const * const p = &x // const pointer to const intint * const p = &x // const pointer to non-const intint const * p = &x // non-const pointer to non-const int


To make the babel complete, const and volatile are distinct but not exclusive:

int main () {        int x;        int const * const a = &x                    // const ptr -> const int        int * const b = &x                          // const ptr -> int        int const * c = &x                          // ptr       -> const int        int volatile const * const d = &x           // const ptr -> const volatile int        int volatile * const e = &x                 // const ptr -> volatile int        int volatile const * f = &x                 // ptr                -> const volatile int        int const * const volatile g = &x           // const volatile ptr -> const int        int * const volatile h = &x                 // const volatile ptr -> int        int const * volatile i = &x                 // volatile ptr       -> const int        int volatile const * const volatile k = &x  // const volatile ptr -> const volatile int        int volatile * const volatile l = &x        // const volatile ptr -> volatile int        int volatile const * volatile m = &x        // volatile ptr -> const volatile int        int volatile * n = &x                       // ptr          -> volatile int        int * volatile o = &x                       // volatile ptr -> int        int volatile * volatile p = &x              // volatile ptr -> volatile int}


Yay.

It's easier by the way if you read right to left (e.g. the last one: p is a volatile pointer to volatile int), using above writing style.

[Edited by - phresnel on November 9, 2009 2:18:20 AM]

This topic is closed to new replies.

Advertisement