473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Declarator operators

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:

-
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

Dec 6 '05 #1
7 4365
On 6 Dec 2005 08:08:39 -0800, "an******@gmail.com"
<an******@gmail.com> wrote:
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.


*kings[] by itself is not a valid expression.

class King { /*...*/ };

King kings[10]; // declares an array of 10 King objects
King *pkings[10]; // declares an array of 10 pointers to King

King k1(kings[0]); // constructs k1 with 1st element of kings[]
King k2(*pkings[0]); // constructs k2 by dereferencing
// 1st element of pkings[]

Is that somewhat clearer?

--
Bob Hairgrove
No**********@Home.com
Dec 6 '05 #2
an******@gmail.com wrote:
....
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.


The trick in reading these is to read decls from the inside out (from
the "thing" being declared.

i.e. "somthing (*kings)[]" == kings -> * -> [] -> something

i.e. kings is a pointer to an array of somthing.

In this case:

"somthing *kings[]" == kings -> [] -> * -> something
Dec 6 '05 #3
an******@gmail.com wrote:
[..]
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,
No, not an array of kings. "kings" is the identifier. Then you continue
to the right. You see []. That means that 'kings' is an _array_. Then
proceed further to the right until you can't go further. Then go left.
If the declarator is '*kings[]', then you can't go beyond the closing
bracket. 'kings' is an array of unknown dimension. But it's known that
'kings' is an array of (stepping to the left now) _pointers_. To what?
You will have to provide the type specifier now.
and then the "*" binds, making it a pointer to an array of kings.
No, not a pointer to an array. An array of pointers. And not of kings.
'kings' is the identifier.
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.


No. The syntax "(*kings)[]" makes 'kings' a _pointer_ first (because you
cannot go right due to the closing parenthesis, and you have to go left),
and an array second. So 'kings' is (going right - stopping due to the
parenthesis - going left) a pointer to (cannot go left - due to opening
parenthesis - so going right again) an array of unknown dimension.

V
Dec 6 '05 #4
>class King { /*...*/ };

King kings[10]; // declares an array of 10 King objects
King *pkings[10]; // declares an array of 10 pointers to King

King k1(kings[0]); // constructs k1 with 1st element of kings[]
King k2(*pkings[0]); // constructs k2 by dereferencing
// 1st element of pkings[]


While I've always certainly read "King *pkings[10]" as declaring an
array of 10 pointers to King, I am confused how to square this with the
text I posted. If indeed postfix declarator operators bind tighter,
then first the postfix brackets bind:

pkings[10] // an array of 10 somethings

and then the prefix "*" binds:

*pkings[10] // a pointer to an array of 10 somethings

and then the base type:

King *pkings[10] // a pointer to an array of 10 kings

Now, I know my interpretation above is patently incorrect. However I
cannot understand how the text can be interpreted differently. Also as
far as the trick above in reading declarations, where does the basis
for this method come from?

Anjo

Dec 6 '05 #5
Bob Hairgrove wrote:
On 6 Dec 2005 08:08:39 -0800, "an******@gmail.com"
<an******@gmail.com> wrote:

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.

*kings[] by itself is not a valid expression.


No, but it's a valid declarator.
class King { /*...*/ };

King kings[10]; // declares an array of 10 King objects
King *pkings[10]; // declares an array of 10 pointers to King

King k1(kings[0]); // constructs k1 with 1st element of kings[]
King k2(*pkings[0]); // constructs k2 by dereferencing
// 1st element of pkings[]

Is that somewhat clearer?


That's irrelevant.

King King1, King2, *kings[] = { &King1, &King2, 0 };
// ^^^^^^^^

V
Dec 6 '05 #6
Anjo Gasa wrote:
class King { /*...*/ };

King kings[10]; // declares an array of 10 King objects
King *pkings[10]; // declares an array of 10 pointers to King

King k1(kings[0]); // constructs k1 with 1st element of kings[]
King k2(*pkings[0]); // constructs k2 by dereferencing
// 1st element of pkings[]

While I've always certainly read "King *pkings[10]" as declaring an
array of 10 pointers to King, I am confused how to square this with the
text I posted. If indeed postfix declarator operators bind tighter,
then first the postfix brackets bind:

pkings[10] // an array of 10 somethings

and then the prefix "*" binds:

*pkings[10] // a pointer to an array of 10 somethings


But you're suddenly introducing the word "pointer" /closer/ to the name of
what's declared here. That's _tighter_. You need to follow the right
path and keep "an array of 10 somethings" _tighter_. So, it's "an array
of 10 ... pointers". 'Pointers' is _farther_ away from 'pkings' than the
'array of 10'.
and then the base type:

King *pkings[10] // a pointer to an array of 10 kings

Now, I know my interpretation above is patently incorrect. However I
cannot understand how the text can be interpreted differently.
Simply stop stumbling over your own feet. "Tighter" means "tighter". You
talk the talk, but you don't walk the walk.
Also as
far as the trick above in reading declarations, where does the basis
for this method come from?


It's been there since 1970 when declaration syntax was first introduced to
C language. In C++ it comes from C. In C... Well, I don't know, Dennis
Ritchie and Brian Kernighan probably introduced the logic.

V
Dec 6 '05 #7

an******@gmail.com wrote:

[snip]
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,
You mean, "making kings an array",
and then the "*" binds, making it a pointer to an array of kings.
You mean, "making kings an array of pointer".
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


Dec 6 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
by: Vasileios | last post by:
Hello could someone help me please. I have the following class definition #include <ext/hash_set> #include "tool.h" class ToolContainer : public QObject {
0
by: Gil | last post by:
Hope this is the right group to post to. I compiled C++ code that ran fine on Windows with VisualStudio 6.0. I then moved the code to Solaris 9 and compiled with g++, but am getting the...
7
by: xxx | last post by:
I am having difficulty seeing why cv_qualifier, which is used as a type specifier in the declaration section of the ISO/IEC grammar, is defined in the declarator section. A declaration can consist...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
0
by: Alex Sedow | last post by:
Standart describe grammar for events as (in EBNF): event-declaration: "event" type variable-declarators ";" "event" type member-name "{" event-accessor-declarations "}" ...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
7
by: JoseMariaSola | last post by:
The following declaration is valid: struct {int x;}; There's no tag and no variable. Does it has any use? Thanks.
10
by: mdh | last post by:
Sorry in advance if this is really dumb, but I am trying to get my head around exactly what the declarator is. In the FAQ, 1.21, part of it says "C declarations ....come in 2 parts, a base type and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.