elhamura
elhamura

Reputation: 3

pointer to an array of pointer to functions

I would like f to point to an array of f_pointer_type. So what did i miss here ? I'm getting an error 'lvalue required as left operand of assignment'. What does it mean ?

#include <stdio.h>
typedef char* (*f_pointer_type)();
char * retHello()
{
    return "hello";
}
int main()
{
    char (* ( *f())[])(); //Declare f to be a pointer to an array of pointers to function that gets nothing and return a char.
    f_pointer_type funcs[3]; //an array of pointers to function that gets nothing and returns a pointer to char
    f = &funcs; //ERROR : lvalue required as left operand of assignment
    return 0;
}

Upvotes: 0

Views: 123

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409166

If you read about the clockwise/spiral rule you will see that f is actually a function returning a pointer to an array of pointers to functions returning char*. In other words it's a function prototype and not a variable declaration.

Try this instead:

f_pointer_type (*f)[];

Upvotes: 3

Lidong Guo
Lidong Guo

Reputation: 2857

You define f as a function pointer. f is pointer to a function who has void params and return char *

Upvotes: 1

Lundin
Lundin

Reputation: 213513

An array of function pointers is written as char* (*f[])(). You have the parenthesis wrong.

Though it is of course better to always use typedef as in the f_pointer_type funcs[3] example.

Upvotes: 1

Related Questions