Rules for complex declarations

Discussion in 'C Programming' started by masood.iqbal@lycos.com, Jan 23, 2005.

  1. Guest

    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
     
    , Jan 23, 2005
    #1
    1. Advertising

  2. pete Guest

    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
     
    pete, Jan 23, 2005
    #2
    1. Advertising

  3. CBFalconer Guest

    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
     
    CBFalconer, Jan 23, 2005
    #3
  4. CBFalconer wrote:
    > 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.
    >


    <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.

    --
    "Women should come with documentation." - Dave
     
    Jonathan Burd, Jan 23, 2005
    #4
  5. On Sun, 23 Jan 2005 18:41:16 +0530, in comp.lang.c , Jonathan Burd
    <> wrote:

    >CBFalconer wrote:
    >> 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.


    And sometimes usenet servers return a weird error which looks like a fail
    but isn't (I get this occasionally).
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jan 23, 2005
    #5
  6. On 23 Jan 2005 02:00:35 -0800, 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).
    >
    >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>>
     
    Barry Schwarz, Jan 23, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    1
    Views:
    1,159
    Patrick TJ McPhee
    Nov 21, 2003
  2. Rules for complex declarations

    , Jan 23, 2005, in forum: C Programming
    Replies:
    0
    Views:
    312
  3. Rules for complex declarations

    , Jan 23, 2005, in forum: C Programming
    Replies:
    0
    Views:
    283
  4. Rules for complex declarations

    , Jan 23, 2005, in forum: C Programming
    Replies:
    0
    Views:
    266
  5. Rules for complex declarations

    , Jan 23, 2005, in forum: C Programming
    Replies:
    0
    Views:
    356
Loading...

Share This Page