Struct containing a function pointer with itself as a return type in C

The following data structure is obtained:

typedef struct
{
    lamp *lamp;
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    void (*func)(struct event *);
} event;

The last line inside the structure should be a pointer to a function with a return type of void with a pointer to the event as an argument, for example:

void function(event *evt);

Although, I get the following warning message: "its scope is just that definition or declaration, which is probably not what you want." Is this right or wrong?

+5
source share
1 answer

Your structure needs should be defined as follows:

typedef struct event  // <<< note the `event` tag here
{
    lamp *lamp;
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    void (*func)(struct event *);
} event;              // <<< you can still keep `event` as a typedef
                      //     which is equivalent to `struct event`
+7
source

All Articles