For me personally, I still don't like the syntax of having to
associate a pointer type modifier next to the variable declaration.
For me, the difference between char and char* is a difference in type,
and should be reflected in the type portion of the declaration, not a
property of the variable name. I would prefer that 'char* a, b, c;'
declare three char* pointers and just not allow mixed pointer and
regular type declarations on a single line.
Do you feel the same way about array or function declarators? After
all, you're doing the same thing; applying the type modifier to the
variable name, not the type specifier. It may feel more natural
because the "[]" and "()" operators are postfix and there's no
confusion about whether they should be associated with the declarator
or the type specifier, but it's the exact same issue.
I still like the array postfix modifier the way its designed. So in
that sense it's somewhat hypocritical of me to advocate keeping the
pointer modifier to the type portion while still being ok with an
array modifier attached to the variable name. I suppose that it's
simply because the array postfix '[]' is so ubiquitous between many
different languages that it's not easily confused.
Most of my bias is probably due to learning programming from the
Stroustrup's C++ book, where declaring both pointers and references
were modifiers of the type portion of the variable declaration. The
choice of using '&' to declare references means that the potential for
confusion between declaring a reference type and referencing the
address of a variable would be much higher. My guess is that
Stroustrup's intent was to distinguish using the '&' as a prefix
operator to signify taking the address of a variable, from using '&'
as a postfix operator to signify a type modifier used to declare a
'reference to type'.
In the same manner, the style of using '*' in his book is used as a
prefix operator to signify pointer dereference, and as a postfix
operator of a type to modify it to a 'pointer to type'. When I got to
C, now people wanted to use prefix '*' to mean both, which due to my
history I find more confusing, and definitely feel resistance to
adopt.
As far as function pointers, they are in their own category and I'm
not sure whether I like them or not. It certainly took some time to
get used to them, but I can't think of an alternate syntax since
functions declaration themselves have return type prefixes and
argument postfix modifiers. I certain would be resistant to any
change in function pointer or declaration semantics now.
Then you have complex declarations like
int *(*(*f)())[10];
which could not be written as compactly if the dereference operator
was bound to the type specifier as opposed to the declarator (whether
that's a good or bad thing in itself is an open question).
If the dereference operator were made postfix, this whole issue would
go away. And if it were postfix, it would have the same precedence as
"[]", "()", ".", and "->", so you could use operator position to
distinguish between "array of pointer" and "pointer to array", rather
than having to use parentheses to do the grouping:
int x*; // x is a pointer to int
int arr*[10]; // arr is a pointer to a 10-element array of int
int arr[10]*; // arr is a 10-element array of pointer to int
int f*(); // f is a pointer to a function returning int
int f()*; // f is a function returning a pointer to int
Our complex declaration above could be rewritten as
int f*()*[10]*;
which reads pretty easily as "f is a pointer to a function returning a
pointer to a 10-element array of pointer to int".
Of course, this change would probably create as many (if not more)
problems than it solves.
Something that has to be remembered (and isn't emphasized enough in
introductory materials IMO) is that C (*and* C++) declaration syntax
is expression-centric, not object-centric. The reason we declare
pointers as "T *p" is because the type of the *expression* "*p" is
T.
Strange, but it is Stroustrup's coding style in his own book that
advocated postfix modifiers for declaring pointer and reference
variables, so the C++ part of your statement is likely not true. I
don't recall if K&R mentioned if there was any motive behind their
choice for pointer variable declaration syntax. Now pointer syntax
has the weight of history behind it, and I know that my pointer
declaration preferences will likely remain an outlier opinion in C
because of it.
Topic: the only truly unfortunate aspect of C's design is the use of
"=" for assignment and "==" for equality, and that's a lexical issue
rather than syntax. If only the assignment operator had been ":=" or
something similarly unlike the equality operator, then a whole class
of bugs would never have existed.
Seems reasonable. It certainly would have mitigated the whole '0 ==
variable' use, which I can't stand from a style perspective.
Backwards code my read to like I.
Best regards,
John D.