2

Possible Duplicate:
What makes more sense - char* string or char *string?

The syntax for pointers has never made sense to me. I noticed that both of these options work for declaring a pointer. Which one should I be using?

Community
  • 1
  • 1
  • 4
    Whichever one you prefer, so long as you are consistent with the formatting of the rest of the project in which you are working. – James McNellis Nov 04 '11 at 18:44
  • or: http://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type – Flexo Nov 04 '11 at 18:45

8 Answers8

9

The whitespace is insigificant for the C compiler.

The difference matters more if you have multiple declarations on the same line:

int* p1, q1;   // p1 is a pointer to int, q1 is an int.
int *p2, *q2;  // p2 and q2 are both pointers to ints.

Putting the asterisk near to the variable name may help you to remember this. But it's probably better to just declare each variable on its own line.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • It wasn't so long ago I saw a question here on SO where the poster had mixed these two up. For me it's the variable that is the pointer, not the type, which makes me use the second alternative. But that might have something to do with that I was "raised" on old-fashioned C. :-) – Some programmer dude Nov 04 '11 at 18:50
  • I'm trying to learn c and I tend to agree with Mark. Given that `int p1, q1` would be declaring two integers, it was confusing to me that `int* p1, q1` gave me a pointer to AN integer and AN integer. Two lines would be best and less confusing if someone was looking at c code but didn't know syntax. `int *p1; int q1;`. I could see it having use IF `*p1` automatically pointed to the address of `q1` but it still has to be assigned. If you don't know this detail of the syntax, at best `int* p1, q1` could be ambiguous to you. One line may be shorter but two lines to me is more clean and clear. – Dan Dec 20 '18 at 13:45
6

It's a question of style, so you should probably use the same style (consistently) as others working on the same code. If it's just you, pick whichever you like.

Personally would argue for int *pointer because int* a, b; means the same as int *a; int b; not int *a; int *b; as one might think, but this is admittedly a fringe case.

Arkku
  • 41,011
  • 10
  • 62
  • 84
3

The spacing is irrelevant for parsing. There are 3 tokens: int, *, and pointer.

drdwilcox
  • 3,833
  • 17
  • 19
3

They are equivalent, and a matter of personal preference.

For completeness, I would add that if you're declaring two pointers, you will have to write:

int *a, *b;

If you write int *a,b, you end up with one pointer to int and one int.

Vlad
  • 18,195
  • 4
  • 41
  • 71
3

From Microsoft's All-In-One Code Framework Coding Standards:

You should put a space between the * character(s) and the type when specifying a pointer type/variable, but there should be no space between the '*' character(s) and the variable. Setting this rule is to be consistent and uniform in code.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
2

You can also use

int * pointer

it just a matter of personal preference. The c++ compiler will know what you mean, just as long as you have the correct syntax.

minus
  • 706
  • 5
  • 14
0

There is no difference between the two. Use the one that you like more.

robbrit
  • 17,560
  • 4
  • 48
  • 68
0

They are both equal. Choose the one you like the most, or follow the style guidelines if you are editing existing code.