I am trying to make a struct which has a function pointer for a function that takes the same struct as an argument. I have this at the moment.
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(sharedData*);
};
I am getting errors like
error: expected ‘)’ before ‘*’ token
and warnings like
warning: no semicolon at end of struct or union
warning: useless storage class specifier in empty declaration
What am I doing wrong here?
The problem is that when you're using typedef struct
to introduce a new struct
that doesn't require the struct
keyword, you cannot refer to the typedef
-ed name inside the declaration of the struct
. Instead, you need to use the full name for the struct. For example:
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(struct sharedData*);
};
Also, your typedef
statement is currently invalid because you haven't given a name by which to call struct sharedData
. One way to fix this would be as follows:
typedef struct sharedData
{
sem_t* forks;
int id;
void (*forkFunc)(struct sharedData*);
} sharedData;
Now, you can refer to the struct by the full name struct sharedData
or by the shorthand name sharedData
.