-1

Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?
Does it matter where I place the asterisk when declaring pointers in C++?

I searched, but my keywords don't seem to fit. I'm pretty sure this question has already been asked., so point me in some direction if you know a link or something !

My question is, why do so many people use

TypeIdentifier *varname;

Instead of

TypeIdentifier* varname;

which is much more logical to me as the * modifies the type and not the variable name.

Community
  • 1
  • 1
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • 3
    Please use the search before asking new questions - this has been asked and answered *numerous* times on SO. – Brian Roach Nov 09 '11 at 22:21
  • Man, I have to train my keyword skills. Thanks for the link. @mekici Yes, it does. Well, I do. It sometimes really confuses me and I have to take a close look what type this variable exactly is. – Niklas R Nov 09 '11 at 22:24
  • Stroustrup recommends `Type* name` instead of `Type *name` – Seth Carnegie Nov 09 '11 at 22:25

5 Answers5

3

In both C and C++, the declaration syntax (mostly) follows usage.

The declaration

int *n;

can be read as "n is of type int*", or as "*n is of type int". And if you take a look at the language grammar, the latter corresponds more closely to the way it's parsed.

For a type consisting of just a name (like int) and a *, it doesn't make a whole lot of difference, but it matters for more complex declarations.

The usual tendency is to use int *n; in C and int* n; in C++. The latter is because that's Stroustrup's personal preference. His point, I think, is that the "n is of type int*" reading is more natural, and the complexities can be avoided by not writing complex declarations. For example, whichever spacing you prefer, it's better to write

int *a;
int b;

than either

int* a, b;

or

int *a, b;
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

That's because of C's declaration syntax.

int* a, b;

looks as if it defined two variables of type int*, but actually declares a as type int* and b as type int. If you write

int *a, b;

this is obvious. I personally however prefer to just not write such a definition, but instead write

int* a;
int b;

which leaves no doubt about the types.

celtschk
  • 19,311
  • 3
  • 39
  • 64
1

Well, the syntax of language says otherwise. You can easily check it by

Type * var1, var2;

What will happen here?

konrad.kruczynski
  • 46,413
  • 6
  • 36
  • 47
1

The C convention is to attach the * to the variable, because multiple variables in a row don't share the *:

int *a, b; /* a is an int*, while b is an int. */

In C++, the rules are the same, but it is more common to attach the wart to the type, due to the confusing nature of the above declaration, and the consequent practice of declaring only one variable per declaration:

int* a;
int b;

I lean towards the C++ convention, but also allowing multiple non-pointer-or-reference declarations on one line:

int a, b;
int* c;
int* d;
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

Because K&R used Foo *bar and people stuck with it.

moshbear
  • 3,282
  • 1
  • 19
  • 33