Rules for complex declarations

M

masood.iqbal

I have always had trouble correctly interpreting complex type
declarations without using the "declarator parser" program given in
K&R. Then someone taught me about the "Clockwise" Rule. Start with the
declared item's name and "spiral outwards" in a clockwise direction.
With this rule I was able to guess the type correctly about half the
time (which was still a great improvement).

I recently came across an old (June 1987) article in the C User's Group
Newsletter by Andrew Binstock. I learned that I was not applying the
"Clockwise" Rule correctly. So here it goes:
Take any declaration, start with the innermost parantheses (in the
absence of parantheses start with the declared item's name) and work
clockwise through the declaration GOING TO THE RIGHT FIRST.

It's the GOING TO THE RIGHT FIRST part that I was not applying
correctly (even though I was sprialing outwards in the clockwise
direction), hence the problem. Here are some examples from the
article:

char c; a char
char c[]; an array of char
char *c[]; an array of pointers to chars
char *c(); a function returning a pointer to chars
char* c()[]; a function returning a pointer to an array of chars
int *c()(); a function returning a pointer to a function
returning an int
int (*(*c)[])(); a pointer to an array of pointers to functions
returning an int

In a nutshell:
[] => array of
* => pointer to
(...) => function that returns a/an

Masood
 
P

pete

I have always had trouble correctly interpreting complex type
declarations without using the "declarator parser" program given in
K&R.

I see that you have posted this message at least five times
in the past 40 minutes. That's four times too many.
 
C

CBFalconer

I have always had trouble correctly interpreting complex type
declarations without using the "declarator parser" program given in
K&R. Then someone taught me about the "Clockwise" Rule. Start with the
declared item's name and "spiral outwards" in a clockwise direction.
With this rule I was able to guess the type correctly about half the
time (which was still a great improvement).

This one is cleaner, but is still posted 5 times in just over one
hour. What can be the purpose of that. It is extremely annoying.
 
J

Jonathan Burd

CBFalconer said:
This one is cleaner, but is still posted 5 times in just over one
hour. What can be the purpose of that. It is extremely annoying.

<ot>

Sometimes after posting, my posts don't appear for a while.
It happened to me once. The poster ends up thinking his
article wasn't posted and posts again. I think this is what has
happened.

(Incidentally, changing the NG server helped).

</ot>

Regards,
Jonathan.
 
M

Mark McIntyre

Sometimes after posting, my posts don't appear for a while.
It happened to me once. The poster ends up thinking his
article wasn't posted and posts again. I think this is what has
happened.

And sometimes usenet servers return a weird error which looks like a fail
but isn't (I get this occasionally).
 
B

Barry Schwarz

I have always had trouble correctly interpreting complex type
declarations without using the "declarator parser" program given in
K&R. Then someone taught me about the "Clockwise" Rule. Start with the
declared item's name and "spiral outwards" in a clockwise direction.
With this rule I was able to guess the type correctly about half the
time (which was still a great improvement).

I recently came across an old (June 1987) article in the C User's Group
Newsletter by Andrew Binstock. I learned that I was not applying the
"Clockwise" Rule correctly. So here it goes:
Take any declaration, start with the innermost parantheses (in the
absence of parantheses start with the declared item's name) and work
clockwise through the declaration GOING TO THE RIGHT FIRST.

It's the GOING TO THE RIGHT FIRST part that I was not applying
correctly (even though I was sprialing outwards in the clockwise
direction), hence the problem. Here are some examples from the
article:

char c; a char
char c[]; an array of char
char *c[]; an array of pointers to chars
char *c(); a function returning a pointer to chars
char* c()[]; a function returning a pointer to an array of chars
int *c()(); a function returning a pointer to a function
returning an int

This one looks like a syntax error. For the description you provide,
you need
int (*c())();
int (*(*c)[])(); a pointer to an array of pointers to functions
returning an int

In a nutshell:
[] => array of
* => pointer to
(...) => function that returns a/an

Masood



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top