views:

54

answers:

1

Hi All,

typedef void (callback)(int *p1, sStruct *p2);

typedef struct _sStruct
{
callback *funct;
}sStruct;

I have the following declaration, in C. How can I compile this recurrent declaration without receiving any error ?

For the moment I receive: syntax error before '*' token on first line.

+11  A: 

You can forward-declare the structure:

/* Tell the compiler that there will be a struct called _sStruct */
struct _sStruct;

/* Use the full name "struct _sStruct" instead of the typedef'ed name
   "sStruct", since the typedef hasn't occurred yet */
typedef void (callback)(int *p1, struct _sStruct *p2);

/* Now actually define and typedef the structure */
typedef struct _sStruct
{
  callback *funct;
} sStruct;

Edit: Updated to match the question's change of type names.

Also, I strongly suggest that you do not give the struct the identifier _sStruct. Global names beginning with a _ are reserved names, and using them for your own identifiers could cause undefined behavior.

Tyler McHenry
You can also forward declare the `typedef` in one go, something like `typedef struct sStruct sStruct;`. And don't hesitate to give the struct and the `typedef` the same name.
Jens Gustedt