Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array of N pointers to functions returning pointers to functions

Tags:

c

types

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?

like image 783
Vijay Avatar asked Feb 03 '10 14:02

Vijay


People also ask

How do you declare an array of N pointers to function returning pointers?

Here's a straightforward, mechanical method for figuring out hairy declarations: a -- a a[N] -- is an N-element array *a[N] -- of pointers (*a[N])() -- to functions *(*a[N])() -- returning pointers (*(*a[N])())() -- to functions *(*(*a[N])())() -- returning pointers char *(*(*a[N])())() -- to char.

How do you return pointers from functions?

Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn't accept such a return type for a function, so we need to define a type that represents that particular function pointer.

Which of the following declares a pointer to a function that returns a pointer to an integer?

int (*f) (int * ): A pointer to a function that takes an integer pointer as argument and returns an integer.

How do you pass an array to a function with pointer?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun(). The below example demonstrates the same.


2 Answers

Typedefs are for wusses. Here's a straightforward, mechanical method for figuring out hairy declarations:

          a                 -- a
          a[N]              -- is an N-element array
         *a[N]              -- of pointers
        (*a[N])()           -- to functions
       *(*a[N])()           -- returning pointers
      (*(*a[N])())()        -- to functions
     *(*(*a[N])())()        -- returning pointers
char *(*(*a[N])())()        -- to char.  

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

 NameFuncPickerPointer a[N];

gives me no information on how to actually use a[i]. I don't know that a[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 typedef

typedef char *NameFunc();
typedef NameFunc *NameFuncPicker();
typedef NameFuncPicker *NameFuncPickerPointer;

and 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

char *theName = (*(*a[i])())();
like image 173
John Bode Avatar answered Oct 10 '22 02:10

John Bode


typedef char* (* tCharRetFunc)();
typedef tCharRetFunc (* tFuncRetCharFunc)();

tFuncRetCharFunc arr[N];
like image 21
Christopher Avatar answered Oct 10 '22 04:10

Christopher