简体   繁体   中英

Why can't I initialize my array of structures containing function pointers in C?

my_functions.h

void * f1 (int * param);
void * f2 (int * param);
void * f3 (int * param);
void b1(int * param);
void b2(int * param);
void b3(int * param);

my_prog.c

#include <my_functions.h>

// Typedef my function pointers
typedef void * (*foo)(int*);
typedef void (*bar)(int*);

// Declare the structure that will contain function pointers
typedef struct _my_struct
{
    int tag;
    foo f;
    bar b;
}my_struct;

// Declare and initialize the array of my_struct
my_struct array[] =
{
    {1, f1, b1},
    {2, f2, b2},
    {3, f3, b3}
};

Compiler says:

warning: initialization from incompatible pointer type

I looked at:

But I still can't see what I missed...
For me, all the types and the functions are known by the time I try to initialize my array.
Can't the init be done outside a function ?

[EDIT] I am on Linux, using an embedded version for arm of GCC 4.9.

Ok so my code works just fine on my PC, it is actually the arm-gcc compiler that I use for my embedded target that throw this error because it seems it can't recognize the type of the function pointers without an explicit cast:

Here is the fix:

my_struct array[] =
{
    {1, (foo)f1, (bar)b1},
    {2, (foo)f2, (bar)b2},
    {3, (foo)f3, (bar)b3}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM