Jump to content
  • Advertisement
Sign in to follow this  
Lode

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

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

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
I prefer the third.

int *n, *o, p, q;


The first way doesn’t fit with the above example.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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!