August 12th, 2013, 06:40 PM
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:
How exactly does this syntax work :confused:
char ( * ( *x () )  ) () ;
x: function returning pointer to array of pointer to function returning char
It's from Dennis M.Ritchie's book.
August 12th, 2013, 07:54 PM
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...
August 12th, 2013, 08:03 PM
Thanks, i'll try to work it out :)
August 12th, 2013, 08:23 PM
Is that The C Programming Language 2nd ed, Kernighan and Ritchie? Page 126?
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.
Originally Posted by K&R, pg 126
August 12th, 2013, 08:40 PM
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 :)
August 12th, 2013, 09:06 PM
They give the grammar on the bottom of page 122.
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.
dcl: optional *'s direct-dcl
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.
August 12th, 2013, 09:51 PM
Thanks a lot. Very informative.
I'll take it step by step.