This was asked to me in an interview!
i really got confused
- How do I declare an array of N
pointers to functions returning
pointers to functions returning
pointers to characters
could anybody please help?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Typedefs are for wusses. Here’s a straightforward, mechanical method for figuring out hairy declarations:
So, the answer is in the neighborhood of
char *(*(*a[N])())();. I say “in the neighborhood” since it’s never specified what arguments the functions take.It’s an obnoxious interview question (types this ugly are truly rare IME), but it does give the interviewer an idea of how well you understand declarators. Either that or they were bored and just wanted to see if they could make your brain sieze.
EDIT
Most everyone else recommends using typedefs. The only time I recommend using a typedef is if the type is intended to be truly opaque (i.e., not manipulated directly by the programmer, but passed to an API, sort of like the FILE type). Otherwise, if the programmer is meant to manipulate objects of that type directly, then IME it’s better to have all that information available in the declaration, ugly as it may be. For example, something like
gives me no information on how to actually use
a[i]. I don’t know thata[i]is callable, or what it returns, or what arguments it should take (if any), or much of anything else. I have to go looking for the typedefand from that puzzle out how to write the expression that actually calls one of the functions. Whereas using the “naked”, non-typedef’d declaration, I know immediately that the structure of the call is