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

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,151
    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:
    281
  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:
    5
    Views:
    435
    Barry Schwarz
    Jan 23, 2005
Loading...

Share This Page