Vindhya G
Vindhya G

Reputation: 1369

function pointer declarations in c

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];

Upvotes: 0

Views: 156

Answers (1)

Pavan Manjunath
Pavan Manjunath

Reputation: 28535

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.

Upvotes: 1

Related Questions