I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such t
There are two ways to do it — for both your example structures. They are essentially isomorphic.
Use a structure tag, as already shown in various other answers.
typedef struct pr_PendingResponseItem
{
// some fields required for processing...
int (*doAction)(struct pr_PendingResponseItem *pr);
} pr_PendingResponseItem;
typedef struct LinkedItem
{
struct LinkedItem *prev;
struct LinkedItem *next;
void * data;
} LinkedItem;
Use typedef to give a name to an incomplete structure type and use the typedef in the structure definition.
typedef struct pr_PendingResponseItem pr_PendingResponseItem;
struct pr_PendingResponseItem
{
// some fields required for processing...
int (*doAction)(pr_PendingResponseItem *pr);
};
typedef struct LinkedItem LinkedItem;
struct LinkedItem
{
LinkedItem *prev;
LinkedItem *next;
void * data;
};
Note that the structure tags are in a different namespace from the typedef names, so there is no need to use different names for the typedef and structure tag. Also note that in C++ the typedef would be unnecessary.