This post is about understanding details in two pieces of C code. Why this is ok:
typedef struct _api_t api_t;
typedef void (*api_set) (api_t * api;
int a;
);
typedef int (*api_read) (api_t * api;
);
struct _api_t
{
api_set set;
api_read read;
};
and this isn't
typedef struct _api_t
{
api_set set;
api_read read;
}
api_t;
typedef void (*api_set) (api_t * api;
int a;
);
typedef int (*api_read) (api_t * api;
);
error: unknown type name ‘api_set’, error: unknown type name ‘api_read’
These records
typedef void (*api_set) (api_t * api;
int a;
);
typedef int (*api_read) (api_t * api;
);
are incorrect. The compiler should issue error messages.
It seems you mean the following code
typedef struct _api_t api_t;
typedef void (*api_set) (api_t * api, int a );
typedef int (*api_read) (api_t * api );
struct _api_t
{
api_set set;
api_read read;
};
This code is correct because in this structure definition
struct _api_t
{
api_set set;
api_read read;
};
the names api_set
and api_read
(defined as function pointers in preceding typedefs) are already defined before they are used in the structure definition.
As for the structure definition in the second code snippet then the names api_set
and api_read
used in the structure definition are not yet defined
typedef struct _api_t
{
api_set set;
api_read read;
}
api_t;
So the compiler will issue error messages that these names are not defined.