Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Rules for complex declarations

Reply
Thread Tools

Rules for complex declarations

 
 
masood.iqbal@lycos.com
Guest
Posts: n/a
 
      01-23-2005
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

 
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
Rules for complex declarations masood.iqbal@lycos.com C Programming 5 01-23-2005 06:50 PM
Rules for complex declarations masood.iqbal@lycos.com C Programming 0 01-23-2005 09:55 AM
Rules for complex declarations masood.iqbal@lycos.com C Programming 0 01-23-2005 09:29 AM
Rules for complex declarations masood.iqbal@lycos.com C Programming 0 01-23-2005 09:26 AM
When to use complex declarations like the following.....? Alan Holloway C Programming 7 08-05-2004 10:52 AM



Advertisments