#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    Need help understanding short but complex declaration


    Can anybody please explain to me, step by step, and why each step is AS THEY SAY IT IS, the following declaration:

    Code:
    char ( * ( *x () ) [] ) () ;
    
    x: function returning pointer to array[] of pointer to function returning char
    How exactly does this syntax work :confused:

    It's from Dennis M.Ritchie's book.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    2
    You must ALWAYS move up by steps.

    Also what you did is function()(); which is not correct.

    It's char ( * ( *x () ) [] );

    First step: char(). What is it? A function that returns a char.
    Second step: *()[]. What is it? An array of void pointers.
    Third step : *x(). What is it? A function that returns a void pointer.
    You put everything together, while remembering that everything beetween "()" is an argument of a function.
    You get: a function that returns a char, and takes for argument: a function that returns a pointer to an array of pointers.

    I hope what I said was correct, if somebody can correct my message...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Thanks, i'll try to work it out :)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    Is that The C Programming Language 2nd ed, Kernighan and Ritchie? Page 126?
    Originally Posted by K&R, pg 126
    char (*(*x())[])()
    That is at the end of section 5.12, Complicated Declarations, which starts on page 122. On page 123 the authors demonstrate how to construct a parsing tree for such expressions.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Yep, that's the one.

    By the way, what is "dcl" and "direct-dcl"? They described it shortly, but i think my understanding of computers isn't deep enough to catch it on from that short explanation.

    Thought i could use a more friendly explanation to start with :)
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    They give the grammar on the bottom of page 122.
    Code:
    dcl:           optional *'s direct-dcl
    direct-dcl:  name 
                    (dcl)
                    direct-dcl()
                    direct-dcl[optional size]
    Then they explain what that says in words. IOW, wherever you see dcl in an expression, you can substitute the definition for it and wherever you see direct-dcl you can substitute one of the definitions for it.

    x is a name, so that's a direct-dcl
    x() is therefore a direct-dcl -- by the direct-dcl() rule
    *x() is a dcl, obviously

    That leaves us with -- char (*(dcl)[])()
    (dcl) is a direct-dcl
    (dcl)[] is also a direct-dcl

    That leaves us with -- char(*direct-dcl)()
    *direct-dcl is a dcl
    so (*direct-dcl) is a direct-dcl

    Now we're down to char direct-dcl()
    direct-dcl() is a direct-dcl.
    I'm not completely sure what to make of char here.

    So what we have here is that the function returns char. I'll leave it for you to draw out a more complete parsing tree and to work out the rest (for one thing, I gotta go to class soon). The tree will show you what is bound more closely.

    And just in case, the code listings in that section are for routines to parse a function pointer declaration, plus one to construct a declaration.

    And remember that the authors do say right up front that it can get confusing.

    If you can get your hands on a Schaum's Outlines Programming With C, at the end of Chapter Ten, the chapter on pointers, they provide a long list of various pointer declarations, including function pointers. In practice (we use function pointers to construct a jump table), we keep our function pointer declarations simple.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Thanks a lot. Very informative.

    I'll take it step by step.

IMN logo majestic logo threadwatch logo seochat tools logo