views:

299

answers:

7

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?

+3  A: 
typedef char* (* tCharRetFunc)();
typedef tCharRetFunc (* tFuncRetCharFunc)();

tFuncRetCharFunc arr[N];
Christopher
I think the question is designed to see if the interviewee understands typedef and give a clear-humane solution like this one or a technical-nasty solution like those given in other answers. As such it is a fairly good question as these things go.
Elemental
A: 

Divide big problem into smaller parts:

/* char_func_ptr is pointer to function returning pointer to char */
typedef char* (*char_func_ptr)();

/* func_func_ptr is a pointer to function returning above type */
typedef char_func_ptr (*func_func_ptr)();

/* the_array is array of desired function pointers */
func_func_ptr the_array[42];
Nikolai N Fetissov
A: 

Is this what you are looking for:

typedef char* charptr;
typedef charptr (*innerfun)();
typedef innerfun (*outerfun)();

const size_t N = 10;
outerfun my_outerfun_array[N];

I hope I got it correct, it seems a strange question to me especially in an interview :(

AraK
A: 

Using typedefs as Christopher tells you is really th only humane way of declaring such a thing. Without tyedefs , it'll become:

char *(*(*arr[10])(void ))(void );

(yes I had to cheat and ran cdecl> declare arr as array 10 of pointer to function(void) returning pointer to function(void) returning pointer to char )

nos
+11  A: 

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])())();
John Bode
Now this **I** even understand without being a c programmer.
Lieven
On the subject of names, I guess I'd like to see `StringGetter_Getter a[N]`, or `StringGetter_Getter_Array a`, or `FunctionTable a`. It must have some meaning in the context of the code, and I'd rather see `StringGetter_Getter` repeated all over the place, than `char *(*(*)())()`. But this could easily devolve into the same argument as whether to name constants: "I don't want to have to go look up the value of DEFAULT_HTTP_PORT, if someone is using 80, I want to see 80 in the source". As programmers, indirection is the cause and solution of all our problems.
Steve Jessop
... and ultimately, if your C code has tables of factory functions that return factory functions, then you probably took a wrong turn on your way to the "Painfully Complex Enterprise Java" class ;-)
Steve Jessop
A: 

I think this will help you. Its from cppreference wiki and explains how to read type declarations.

For a detailed explanation there is another site.

coelhudo
A: 

thanks for all good explaination. ya using typdef is more convinient

Riyaz