A
anjogasa
In the midst of reading "The C++ Programming Language", the latest
edition by Stroustrup, and I find myself butting my head against a few
paragraphs. I have searched the errata on the website, and since its
not in error, my understanding must be. I quote the following:
-
A declaration consists of four parts: an optional "specifier", a base
type, a declarator, and an optional initializer.
[...]
A declarator is composed of a name and optionally some declarator
operators. The most common delcarator operators are:
* - pointer - prefix
*const - constant pointer - prefix
& - reference - prefix
[] - array - postfix
() - funtion - postfix
Their use would be simple if they were all either prefix or postfix.
However, *, [], () were designed to mirror their use in expressions.
Thus, * is prefix and [] and () are postfix. The postfix declarator
operators bind tigther than the prefix ones. Consequently, *kings[] is
an array of pointers to something, and we have to use parentheses to
express types such as "pointer to function".
-
My questions involves the sentences involving the declarator
precedence. The passage states that the postfix declarator operators,
[] and (), bind tighter than the prefix declarator operators, *,
*const, and &. The way I understand this as to how it would apply to
"*kings[]", is that the "[]" binds first, making it an array of kings,
and then the "*" binds, making it a pointer to an array of kings. The
book states though that it is "an array of pointers to something". It
would seem that "(*kings)[]" would be an array of pointers to
something, not the first example.
Anjo
edition by Stroustrup, and I find myself butting my head against a few
paragraphs. I have searched the errata on the website, and since its
not in error, my understanding must be. I quote the following:
-
From 4.9.1 - The Structure of a Declaration
A declaration consists of four parts: an optional "specifier", a base
type, a declarator, and an optional initializer.
[...]
A declarator is composed of a name and optionally some declarator
operators. The most common delcarator operators are:
* - pointer - prefix
*const - constant pointer - prefix
& - reference - prefix
[] - array - postfix
() - funtion - postfix
Their use would be simple if they were all either prefix or postfix.
However, *, [], () were designed to mirror their use in expressions.
Thus, * is prefix and [] and () are postfix. The postfix declarator
operators bind tigther than the prefix ones. Consequently, *kings[] is
an array of pointers to something, and we have to use parentheses to
express types such as "pointer to function".
-
My questions involves the sentences involving the declarator
precedence. The passage states that the postfix declarator operators,
[] and (), bind tighter than the prefix declarator operators, *,
*const, and &. The way I understand this as to how it would apply to
"*kings[]", is that the "[]" binds first, making it an array of kings,
and then the "*" binds, making it a pointer to an array of kings. The
book states though that it is "an array of pointers to something". It
would seem that "(*kings)[]" would be an array of pointers to
something, not the first example.
Anjo