C Declaration Puzzle

P

pingkai

Hi,

From the FAQ I know that "the array of N pointers to functions
returning pointers to functions returning pointers to char" is
declared as
char * (* (*a[N]) ( ) ) ( ) ;
And I can understand the declaration using Clockwise/Spiral Rule.

However I am wondering why I can not declare it like this:
char* (* ( *a[N]) ( ) ( ) ) ;

It seems from the rule, it will also give out the same definition,
however the compiler says it actually means
"a array of N pointers to function returning function returning
pointer to pointer to char" and is thus an error.

Could anybody explain this? Thanks.
 
B

Ben Pfaff

pingkai said:
From the FAQ I know that "the array of N pointers to functions
returning pointers to functions returning pointers to char" is
declared as
char * (* (*a[N]) ( ) ) ( ) ;
And I can understand the declaration using Clockwise/Spiral Rule.

However I am wondering why I can not declare it like this:
char* (* ( *a[N]) ( ) ( ) ) ;

The two empty ()s apply sequentially. It's the same reason that
int *x[5][4] is an array of arrays, not an array of pointers to
arrays.
 
B

Ben Bacarisse

pingkai said:
Hi,

From the FAQ I know that "the array of N pointers to functions
returning pointers to functions returning pointers to char" is
declared as
char * (* (*a[N]) ( ) ) ( ) ;
And I can understand the declaration using Clockwise/Spiral Rule.

However I am wondering why I can not declare it like this:
char* (* ( *a[N]) ( ) ( ) ) ;

It seems from the rule, it will also give out the same definition,
however the compiler says it actually means
"a array of N pointers to function returning function returning
pointer to pointer to char" and is thus an error.

Could anybody explain this? Thanks.

You always read to the right first and only read to the left when hit
an unmatched ) or the end of the declarator (not the declaration).
You stop reading to the left when you hit the matching ( or the start.

After you've finished with a nested, bracketed, part, you remove it
start again trying to read to the right. This rule agrees with the
compiler. If the rule you are using does not, ditch it!

In this case:

char * (* (*a[N]) () () ) ; <<<<< "pointers to"
char * (* () () ) ; <<<<<<<<<<<<<<<< "a pointer to"
char * ;<<<<<<<<<<<<<<<<<<<<<<<<<< "pointer to char"

Of course, in C, functions can't return functions.

I can't explain why the rule you are using does not work because I
don't know it. Where does it come from?
 
P

pingkai

Thanks for the explanation. I got the rule from here:
http://c-faq.com/decl/spiral.anderson.html

It seems that it does not say that one needs to read right as far as
possible before reaching unmatched ) or the end of the declarator.
However your explanation works very well, thanks.

P.

pingkai said:
From the FAQ I know that "the array of N pointers to functions
returning pointers to functions returning pointers to char" is
declared as
char * (* (*a[N]) ( ) ) ( ) ;
And I can understand the declaration using Clockwise/Spiral Rule.
However I am wondering why I can not declare it like this:
char* (* ( *a[N]) ( ) ( ) ) ;
It seems from the rule, it will also give out the same definition,
however the compiler says it actually means
"a array of N pointers to function returning function returning
pointer to pointer to char" and is thus an error.
Could anybody explain this? Thanks.

You always read to the right first and only read to the left when hit
an unmatched ) or the end of the declarator (not the declaration).
You stop reading to the left when you hit the matching ( or the start.

After you've finished with a nested, bracketed, part, you remove it
start again trying to read to the right.  This rule agrees with the
compiler.  If the rule you are using does not, ditch it!

In this case:

char * (* (*a[N]) () () ) ;
            >>>>            "a is an array of N"
           <<<<<            "pointers to"
char * (*         () () ) ;
          >>>>>>>>>>>>>>    "functions returning functions returning"
        <<<<<<<<<<<<<<<<    "a pointer to"
char *                    ;
        >>>>>>>>>>>>>>>>>>  ""
<<<<<<<<<<<<<<<<<<<<<<<<<<  "pointer to char"

Of course, in C, functions can't return functions.

I can't explain why the rule you are using does not work because I
don't know it.  Where does it come from?
 
B

Ben Bacarisse

pingkai said:
On Aug 11, 6:59 pm, Ben Bacarisse <[email protected]> wrote:
In this case:

char * (* (*a[N]) () () ) ;
            >>>>            "a is an array of N"
           <<<<<            "pointers to"
char * (*         () () ) ;
          >>>>>>>>>>>>>>    "functions returning functions returning"
        <<<<<<<<<<<<<<<<    "a pointer to"
char *                    ;
        >>>>>>>>>>>>>>>>>>  ""
<<<<<<<<<<<<<<<<<<<<<<<<<<  "pointer to char"

Of course, in C, functions can't return functions.

I can't explain why the rule you are using does not work because I
don't know it.  Where does it come from?

[Top posting fixed]
Thanks for the explanation. I got the rule from here:
http://c-faq.com/decl/spiral.anderson.html

I prefer my own explanation (I would, of course, it's how I got to
understand it) but I don't think there is anything wrong with the
"spiral" idea as presented there.
It seems that it does not say that one needs to read right as far as
possible before reaching unmatched ) or the end of the declarator.

It does explain the ()s thing -- second example, first bullet point --
but only by way of example which does not always make the general rule
clear.
However your explanation works very well, thanks.

I'm glad it helped.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top