Function pointer in the structure with the same type of argument

I am trying to create a structure that has a pointer to a function that takes the same structure as the argument. I have this at the moment.

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(sharedData*);
};

I get errors like

error: expected ') before' * token

and warnings for example

 warning: no semicolon at end of struct or union 
 warning: useless storage class specifier in empty declaration

what am i doing wrong here?

+3
source share
1 answer

The problem is that when used typedef structto introduce a new structone that does not require a keyword struct, you cannot refer to the typedef-ed name inside the declaration struct. Instead, you need to use the full name for the structure. For instance:

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(struct sharedData*);
};

, typedef , , struct sharedData. :

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(struct sharedData*);
} sharedData;

struct sharedData sharedData.

+6

Source: https://habr.com/ru/post/1789921/


All Articles