0

I was going through k&r complicated declarations part.I got doubt about this particular declaration.

char(*(*x[3])())[5]

Why cant it be char[5] (*(*x[3])()) And can this declaration be legal?

 int* (*(*x)())[2];
5
  • 2
    See e.g. the clockwise/spiral rule. Commented Jun 26, 2012 at 11:52
  • 1
    It is bad syntax, since [] must come after the name. Commented Jun 26, 2012 at 11:52
  • The syntax of C declarations was (perhaps unwisely) defined to imitate use. You're declaring an array of function pointers, and the functions return array pointers. To call the first function in the array, and get the first char value from what it returns, you could write (*(*x[0])())[0], and that's why the [] goes afterwards in the declaration too. Commented Jun 26, 2012 at 12:01
  • Please note that there is limited practical use of obscure declarations like these. In any program from the real world, typedefs would have been used instead. Commented Jun 26, 2012 at 12:23
  • yes.but its just for learning purpose Commented Jun 26, 2012 at 12:26

1 Answer 1

1

According to the precedence of operators and applying the spiral rule,

char(*(*x[3])())[5]

is equivalent to

x is array of pointers to functions returning pointer to array of char

But in,

char[5] (*(*x[3])())

the array subscript should be at the end of the declaration, thus resulting in a syntax error. You'll bump into nothing when you apply spiral rule to this.

Also,

int* (*(*x)())[2]; 

is perfectly legal and its declaration can be stated as

x is pointer to function returning pointer to array of pointer to int

Check out the Java applet which can help you decode complicated declarations and also read these articles of how to form complicated declarations.

@Steve Jessop's comment also seems plausible as to why the [] go at the end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.