A lot of the arguments in this topic are plain subjective and the argument about "the star binds to the variable name" is naive. Here's a few arguments that aren't just opinions:
The forgotten pointer type qualifiers
Formally, the "star" neither belongs to the type nor to the variable name, it is part of its own grammatical item named pointer. The formal C syntax (ISO 9899:2018) is:
(6.7) declaration:
declaration-specifiers init-declarator-listopt ;
Where declaration-specifiers contains the type (and storage), and the init-declarator-list contains the pointer and the variable name. Which we see if we dissect this declarator list syntax further:
(6.7.6) declarator:
pointeropt direct-declarator
...
(6.7.6) pointer:
*
type-qualifier-listopt
*
type-qualifier-listopt pointer
Where a declarator is the whole declaration, a direct-declarator is the identifier (variable name), and a pointer is the star followed by an optional type qualifier list belonging to the pointer itself.
What makes the various style arguments about "the star belongs to the variable" inconsistent, is that they have forgotten about these pointer type qualifiers. int* const x
, int *const x
or int*const x
?
Consider int *const a, b;
, what are the types of a
and b
? Not so obvious that "the star belongs to the variable" any longer. Rather, one would start to ponder where the const
belongs to.
You can definitely make a sound argument that the star belongs to the pointer type qualifier, but not much beyond that.
The type qualifier list for the pointer can cause problems for those using the int *a
style. Those who use pointers inside a typedef
(which we shouldn't, very bad practice!) and think "the star belongs to the variable name" tend to write this very subtle bug:
/*** bad code, don't do this ***/
typedef int *bad_idea_t;
...
void func (const bad_idea_t *foo);
This compiles cleanly. Now you might think the code is made const correct. Not so! This code is accidentally a faked const correctness.
The type of foo
is actually int*const*
- the outer most pointer was made read-only, not the pointed at data. So inside this function we can do **foo = n;
and it will change the variable value in the caller.
This is because in the expression const bad_idea_t *foo
, the *
does not belong to the variable name here! In pseudo code, this parameter declaration is to be read as const (bad_idea_t *) foo
and not as (const bad_idea_t) *foo
. The star belongs to the hidden pointer type in this case - the type is a pointer and a const-qualified pointer is written as *const
.
But then the root of the problem in the above example is the practice of hiding pointers behind a typedef
and not the *
style.
Regarding declaration of multiple variables on a single line
Declaring multiple variables on a single line is widely recognized as bad practice1). CERT-C sums it up nicely as:
DCL04-C. Do not declare more than one variable per declaration
Just reading the English, then common sense agrees that a declaration should be one declaration.
And it doesn't matter if the variables are pointers or not. Declaring each variable on a single line makes the code clearer in almost every case.
So the argument about the programmer getting confused over int* a, b
is bad. The root of the problem is the use of multiple declarators, not the placement of the *
. Regardless of style, you should be writing this instead:
int* a; // or int *a
int b;
Another sound but subjective argument would be that given int* a
the type of a
is without question int*
and so the star belongs with the type qualifier.
But basically my conclusion is that many of the arguments posted here are just subjective and naive. You can't really make a valid argument for either style - it is truly a matter of subjective personal preference.
1) CERT-C DCL04-C.
typedefs
, but that will add unnecessary complexity, IMHO.typedef
ing pointers is a good idea – this just hides information without any further benefit apart from avoiding variable declaration errors – and for the latter the better approach is not to define more than one single variable at one line of code (while acknowledging that this produces a bit more typing and some more lines of code, but that's still better than thetypedef
...).