Note: This question is about the position of the asterisk (*
).
In most C code I see (e.g., in Beej\'s guide to network programming), all variable declar
A couple of people mentioned very good points. I just want to note that while C has kept the meaning "declaration reflects use" very strongly, C++ didn't. Several declarators in C++ don't reflect the use in declarations;
int &ref; // but "&ref" has type "int*", not "int".
int &&ref; // but "&&ref" is not valid at all.
I don't think that C++ programmers writing it different actually have that as their reason, but it may be something stopping C++ book writers from mentioning it, which may influence those C++ programmers.
From Stroustrup's C++ Style and Technique FAQ.
A "typical C programmer" writes
int *p;
and explains it "*p
is what is theint
" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the*
binds to the namep
in the grammar.A "typical C++ programmer" writes
int* p;
and explains it "p
is a pointer to anint
" emphasizing type. Indeed the type ofp
isint*
. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
Personally, I would disagree with this -- I don't think there is a C or C++ way of doing this. I've never seen any evidence to suggest that. However, I do have a hypothesis as to why the syntax with the asterisk closer to the declared variable name in C.
In C (at least, in C89, the vast majority of C in use), you must declare all your variables at the top of a block. This leads to programmers doing
T a, b, c;
relatively often, while in C++ this is rare. (In C++ you declare variables close to where they are used, and variables often have constructor parameters, etc; therefore it's rare to declare multiple objects on a single line)
This also happens to be the only case where the syntax matters, because the statement
T* a, b, c;
declares one pointer, rather than the three the programmer might expect.
Since putting more than one declaration on a single line is more common in C, and also is the only case where the position of the asterisk matters, it's more likely for the programmer to associate the asterisk with the variable.
That said, I've never seen any evidence of this -- this is just conjecture.
Just to throw some controversy into the matter:
int* a; int* b;
int *a;
int* a, * b, * c;
int* (* func)()
.There are other elements of C that are incorrectly used, such as const
, which belongs on the right too! int const a;
. This is commonly put on the left of the type, despite belonging to the type to its left. Many compilers have to be modified to handle this usage. In C++ you can observe this is conformed to with constant methods, as the compiler can't deduce for you if it's put on the left: int A::a() const
. This also breaks down with nested usage of const
, restricted
and friends with such declarations as int const *
. Conventional usage of const
would suggest this is a constant pointer to an integer, but that's actually int *const
.
You've all been doing it wrong all along, put everything on the right.