473,395 Members | 1,968 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,395 software developers and data experts.

Declarator question

mdh
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 a
declarator......".

The example used is char *pc where the declarator " *pc" tells us
that " *pc is a character".

Can one thus broadly say that a declarator is in fact equivalent to
the base type?

So, in a more complicated expression eg char * ( *pfpc) () ; once
again "* ( *pfpc) ()" is a character?

Thanks as usual.
Jul 16 '08 #1
10 1866
mdh wrote:
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 a
declarator......".

The example used is char *pc where the declarator " *pc" tells us
that " *pc is a character".

Can one thus broadly say that a declarator is in fact equivalent to
the base type?
In `char *pc' the base type is `char' and the declarator is
`*pc'. The declaration -- both pieces taken together -- says that
the declarator has the base type: in this case, `*pc' has the
type `char'. Later in the program, any place you write `*pc' you
have written an expression whose type is `char'.
So, in a more complicated expression eg char * ( *pfpc) () ; once
again "* ( *pfpc) ()" is a character?
Yes. An expression that looks like the declarator has a type,
and that type is the base type from the declaration.

--
Er*********@sun.com
Jul 16 '08 #2
mdh
On Jul 16, 10:54*am, Eric Sosman <Eric.Sos...@sun.comwrote:
>
* * *In `char *pc' the base type is `char' and the declarator is
`*pc'. *The declaration -- both pieces taken together -- says that
the declarator has the base type: in this case, `*pc' has the
type `char'. *Later in the program, any place you write `*pc' you
have written an expression whose type is `char'.


thanks Eric. A little more probing.
Now if one adds a qualifyer like 'const' as in 'const char' etc,is the
type now 'const char' ?


Jul 16 '08 #3
mdh wrote:
On Jul 16, 10:54 am, Eric Sosman <Eric.Sos...@sun.comwrote:
> In `char *pc' the base type is `char' and the declarator is
`*pc'. The declaration -- both pieces taken together -- says that
the declarator has the base type: in this case, `*pc' has the
type `char'. Later in the program, any place you write `*pc' you
have written an expression whose type is `char'.



thanks Eric. A little more probing.
Now if one adds a qualifyer like 'const' as in 'const char' etc,is the
type now 'const char' ?
Yes; it's a "qualified type." The type qualifiers are
const, volatile, and (new in C99) restrict, and they can be
used alone, in combination, or (of course) not at all.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 16 '08 #4
mdh wrote:
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 a
declarator......".

The example used is char *pc where the declarator " *pc" tells us
that " *pc is a character".

Can one thus broadly say that a declarator is in fact equivalent to
the base type?
I would go so far as to say that the declarator is an lvalue
of the base type.

--
pete
Jul 16 '08 #5
mdh
On Jul 16, 3:26*pm, pete <pfil...@mindspring.comwrote:
mdh wrote:
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 a
declarator......".
The example used is char *pc where the declarator *" *pc" tells us
that " *pc is a character".
Can one thus broadly say that a declarator is in fact equivalent to
the base type?

I would go so far as to say that the declarator is an lvalue
of the base type.

--
pete
Thanks Pete and Eric.
Jul 16 '08 #6
On Jul 16, 12:25 pm, mdh <m...@comcast.netwrote:
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 a
declarator......".

The example used is char *pc where the declarator " *pc" tells us
that " *pc is a character".

Can one thus broadly say that a declarator is in fact equivalent to
the base type?

So, in a more complicated expression eg char * ( *pfpc) () ; once
again "* ( *pfpc) ()" is a character?

Thanks as usual.
Pete and Eric have given you good answers, but I'd like to add a few
things.

The declarator introduces the name of the thing being declared (pc)
and any additional type information not provided by the type specifier
"char". In this case, the "pointerness" of pc is provided by the
declarator *pc.

Remember that in C, declaration mimics use. If I have a pointer to a
character, I retrieve the character value by dereferencing the pointer
like so:

c = *pc;

Thus, the expression "*pc" evaluates to a char value. So, going by
the "declaration mimics use" rule, the declaration for a pointer to
char is

char *pc;

Remember that the '*' is bound to the identifier, not the type
specifier, regardless of any whitespace. "char* pc;" is the same as
"char * pc;" and "char *pc;". If you wrote

char* pc1, pc2;

only pc1 would be declared as a pointer to char; pc2 would be a
regular char.

Array types are similar. If I want to retrieve a specific character
value from an array of char, I use the subscript operator:

c = ac[i];

Again, the type of the expression "ac[i]" is char, so the declaration
for an array of char is

char ac[N];

Your function pointer also follows this rule. If I have a pointer to
a function that returns a pointer to a char, I retrieve that char
value by calling that function (using the dereferenced function
pointer), and then dereference the value returned by the function,
like so:

c = *(*pfpc)();

Hence the declaration

char *(*pfpc)();

When you come across a hairier than normal declarator, the way to read
it is to find the leftmost identifier, then work your way out,
remembering that () and [] bind before * (IOW, *a[] is an array of
pointer, not a pointer to an array). Using pfpc as an example:

pfpc -- pfpc
*pfpc -- is a pointer
(*pfpc)() -- to a function
*(*pfpc)() -- returning a pointer
char *(*pfpc)() -- to char.

Note that the type qualifier "typedef" changes things a little. In
the declaration

typedef char *(*pfpc)();

pfpc is not an instance of a pointer to a function to a pointer to
char, but is rather a synonym for the *type* "pointer to a function
returning pointer to char". You could use the typedef to make some
declarations easier to read:

typedef char *charptr; // charptr is a synonym for "char *"
typedef charptr (*fptr)(); // fptr is a synonym for charptr (*)()
fptr myptr; // myptr is a pointer to a function
// returning a pointe to char

although I tend not to do this, as typedefs sometimes obscure more
that they illuminate.
Jul 17 '08 #7
John Bode wrote:

<snip>
Remember that in C, declaration mimics use. If I have a pointer to a
character, I retrieve the character value by dereferencing the pointer
like so:

c = *pc;

Thus, the expression "*pc" evaluates to a char value. So, going by
the "declaration mimics use" rule, the declaration for a pointer to
char is

char *pc;
Sometimes I wish that C had used a specific type specifier to declare
pointers like say 'ptr' as in:

ptr long lp = &some_long_object;

Then we could've done this:

some_other_long_obj = lp;

and perhaps reserved *lp to access value of lp itself.

So much more code deferences pointers than manipulating their values
that this syntax would've led to a more "cleaner" code, IMHO. But OTOH
it would've hidden the fact that an indirection is taking place. Oh
well, it three decades too late now...

<rest of excellent explanations snipped>

Jul 17 '08 #8
mdh
On Jul 17, 9:34*am, John Bode <jfbode1...@gmail.comwrote:
>

Pete and Eric have given you good answers, but I'd like to add a few
things.
John, I can only quote Santosh and say thank you for those "excellent
explanations".
Jul 17 '08 #9
On Thu, 17 Jul 2008 22:45:02 +0530, santosh <sa*********@gmail.com>
wrote in comp.lang.c:
John Bode wrote:

<snip>
Remember that in C, declaration mimics use. If I have a pointer to a
character, I retrieve the character value by dereferencing the pointer
like so:

c = *pc;

Thus, the expression "*pc" evaluates to a char value. So, going by
the "declaration mimics use" rule, the declaration for a pointer to
char is

char *pc;

Sometimes I wish that C had used a specific type specifier to declare
pointers like say 'ptr' as in:

ptr long lp = &some_long_object;

Then we could've done this:

some_other_long_obj = lp;

and perhaps reserved *lp to access value of lp itself.

So much more code deferences pointers than manipulating their values
that this syntax would've led to a more "cleaner" code, IMHO. But OTOH
it would've hidden the fact that an indirection is taking place. Oh
well, it three decades too late now...
No, a thousand times NO!!!

That is actually one of the problem with C++ references. If you are
not looking at the prototype, does:

int x = 42;
some_cpp_func(x);

....pass 'x' by value, making the caller's int immune to changes?

....pass 'x' by non constant reference, allowing the function to change
the caller's object?

....pass 'x' by constant reference, making the caller's int immune to
changes?

Well, which is it???

But even worse, it brings HORRIBLE FLASH BACKS TO PL/M!!!

declare pointer address;
declare data byte based address;

The worst of all possible worlds!

The declaration of the pointer object itself does not tell you that
it's a pointer (in PL/M 80, the one and only 16-bit data type, an
unsigned integer type that also happened to be compatible with a
pointer, had type 'address', even if you only wanted to use it as an
int).

The pointed-to object was accessed by its own name, with no obvious
relationship to the pointer.

You can take away my asterisk when you rip it out of my cold, dead
hands.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 18 '08 #10
On Wed, 16 Jul 2008 18:26:01 -0400, pete <pf*****@mindspring.com>
wrote:
mdh wrote:
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 a
declarator......".

The example used is char *pc where the declarator " *pc" tells us
that " *pc is a character".

Can one thus broadly say that a declarator is in fact equivalent to
the base type?

I would go so far as to say that the declarator is an lvalue
of the base type.
Sort of, but not exactly, nor completely.

In general a declarator is not an expression, only similar to it.

For a pointer declarator T *p, *p is indeed a T lvalue, assuming p has
been set to a valid nonnull value i.e. a pointer to a T object.
(And for T * f(parms?) if f(args) returns such a value.)

For an array declarator T a[6], a[0] or a[5] is, but not a[6].

For a function declarator T f(int,double), f(1, 2.3) is a T _rvalue_
not an lvalue.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 28 '08 #11

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

Similar topics

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...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
3
by: Andreas Leitner | last post by:
Hi, I got a code snipset that gcc refuses to compile. I am note sure however whether this is a bug in gcc, or the snipset is just invalid C code. This is the source code in question: ----...
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 "}" ...
7
by: anjogasa | last post by:
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...
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.
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.