 | | From: | masood.iqbal at lycos.com | | Subject: | Rules for complex declarations | | Date: | 23 Jan 2005 02:00:35 -0800 |
|
|
 | 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
|
|
 | | From: | CBFalconer | | Subject: | Re: Rules for complex declarations | | Date: | Sun, 23 Jan 2005 10:47:08 GMT |
|
|
 | masood.iqbal@lycos.com wrote: > > 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.
-- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
|
|
 | | From: | Jonathan Burd | | Subject: | Re: Rules for complex declarations | | Date: | Sun, 23 Jan 2005 18:41:16 +0530 |
|
|
 | CBFalconer wrote: > masood.iqbal@lycos.com wrote: > >>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. >
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).
Regards, Jonathan.
-- "Women should come with documentation." - Dave
|
|
 | | From: | pete | | Subject: | Re: Rules for complex declarations | | Date: | Sun, 23 Jan 2005 10:24:19 GMT |
|
|
 | masood.iqbal@lycos.com wrote: > > 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.
-- pete
|
|