4

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?

1
  • 2
    Did you mean to write struct sharedData* instead? Commented Feb 4, 2011 at 5:39

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Drop the final underscore; it's just ugly and useless. Using the same name for the struct tag and typedef is perfectly valid.
@R..- Cool! I didn't know that. Will update my original post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.