Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Declarator question

Reply
Thread Tools

Declarator question

 
 
mdh
Guest
Posts: n/a
 
      07-16-2008
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.
 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      07-16-2008
On Jul 16, 10:54*am, Eric Sosman <(E-Mail Removed)> wrote:

>
> * * *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' ?




 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      07-16-2008
On Jul 16, 3:26*pm, pete <(E-Mail Removed)> 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.
>
> --
> pete


Thanks Pete and Eric.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      07-17-2008
On Jul 16, 12:25 pm, mdh <(E-Mail Removed)> 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?
>
> 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-17-2008
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>

 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      07-17-2008
On Jul 17, 9:34*am, John Bode <(E-Mail Removed)> wrote:
>
>
> 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".


 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      07-28-2008
On Wed, 16 Jul 2008 18:26:01 -0400, pete <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C grammar for direct-declarator question Remco van Engelen C Programming 5 09-14-2006 09:51 PM
cv_qualifier in declarator grammar section xxx C++ 7 01-04-2005 09:53 PM
Compile error: invalid type modifier within pointer declarator Per Johansson C++ 3 11-07-2004 09:43 AM
template-id 'VISDictionary<const char *, VISNamedContainerTypeCode *>' used as a declarator Gil C++ 0 12-10-2003 04:52 PM
template-id `hash_set<Tool*>' used as a declarator Vasileios C++ 4 11-04-2003 01:46 PM



Advertisments