0

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.

5
  • Your code works for me. Commented Oct 21, 2014 at 15:14
  • For me too. At which line do you get the error ? Which compiler/platform/OS are you using ? Commented Oct 21, 2014 at 15:26
  • I am reviewing my code as I simplified it in my post... I might have missed something. I get the warning at all the closing braces of the {x,fx,bx} Commented Oct 21, 2014 at 15:28
  • And does your simplified code compile w/o warnings ? Commented Oct 21, 2014 at 15:30
  • Actually it doesn't throw any warning. The compiler I use is to blame, cf my own answer. Commented Oct 21, 2014 at 15:57

1 Answer 1

2

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}
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.