I
ImpalerCore
[snip]
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.
As far as the language grammar is concered, it is. Here's a grammar
trace for a simple pointer declaration "int* p;" (taken fromhttp://www.open-std.org/jtc1/sc22/open/n2356/gram.html):
declaration
|
block-declaration
|
simple-declaration
/ \
decl-specifier-seq init-declarator-list
| |
type-specifier init-declarator
| |
simple-type-specifier declarator
| / \
int ptr-operator declarator
| |
* direct-declarator
|
id-expression
|
unqualified-id
|
identifier
|
p
Thanks for the link.
The '*' operator is bound to the declarator (the trace for a reference
declaration is identical, as '&' is also a production of ptr-
operator). I get what Stroustrup is saying, and I've done enough C++
to see where that approach makes certain things easier (particularly
in a generic container type), but unfortunately the language grammar
doesn't work that way.
I see your point now; it does make sense to declare pointer variables
according to the grammar designed for the C compiler.
In the absence of historical bias, I would still advocate that 'char*
a, b[10], c;' represent
a - char* pointer
b - an array of 10 char* pointers
c - char* pointer
It would require a grammar change to the C language that is never
going to happen. And I'll say that the internals of the compiler are
"deep magic" to me (with an engineering education background), so
unfortunately I'm not well equipped to argue one way or another which
grammar is better (and I have no idea how the above semantics change
the expression parse tree). I do wish I had a better background on
compiler and language design.
I wonder what prompted Stroustrup to use the type postfix location in
his coding style a la 'char* p' or 'int& r' if the grammar itself is
defined otherwise.
Best regards,
John D.