int* n, int * n, or int *n?

Started by
31 comments, last by MrEvil 18 years, 8 months ago
When making a pointer n of type int, which of these 3 styles do you use in your code?

int* n
int * n
int *n
Personally, I prefer the first one, I find it the easiest to understand, because then it reads as "n is of type int*, so it is a pointer" and not as "some confusing variable with the name *n is of type int". But I see that people usually use the third possibility.
Advertisement
I use the first one aswell, as n is of type int* it just makes more sense to me. I think some people prefer the last one because it looks better if your doing something like this

int *n,*m,*o;
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
I use 'int * n;' when declaring a pointer. But when I want to access the actual variable I use '*n = 5;'. Same goes for the address of a variable: '&n'. I just like this style. ;)
Some recent discussion on this topic.

Enigma
I prefer the third.
int *n, *o, p, q;


The first way doesn’t fit with the above example.
I'm using the third possibility:

int *n;

I'm using it because the pointer declaration (*) is bound to the variable name,
and not to the type declaration. It makes more sense when declaring several
variables. I think it shows more clearly which variables are pointers.

int *n, *m, a;

Instead of:

int* n,* m, a;
I like to use all three, in the same function if possible, to keep people on their toes.

(IOW: it doesn't really matter...)
OTOH, we could have

static_cast<int*>(&n)
vector<int*>
void f(int*, char***[], int(*)());

and such constructs. For the first two of those, I prefer using int* rather than int *, since the * doesn't have an identifier to bind to. It depends how you code, as Bjarne Stroustrup says, in C the emphasis is on the *p (int *p), C++ the emphasis is on the types (int*). He expresses this in his C++ style and technique FAQ.

<edit>Bah. Should have read the other thread fully first. [embarrass]</edit>
Quote:Original post by Anonymous Poster
I like to use all three, in the same function if possible, to keep people on their toes.

(IOW: it doesn't really matter...)


The method you use isn't too important (unless you're required to by an employer, etc.), but you should be consistant. I personally hate reading code that completely changes its format every two or three lines, as I'm sure anybody that reads code does.
Quote:Original post by Anonymous Poster
int *n;

int *n, *m, a;


Personally I prefer the first style, and therefore avoid declaring pointers in that fashion, for exactly that reason.

To me, a pointer to a int is the type, so the * belongs to the type declaration.

As someone has already said though, it's all down to personal preference. Just make sure that you're consistent, and stick with whatever style you choose.

This topic is closed to new replies.

Advertisement