Got the following data struct:
typedef struct
{
lamp *lamp;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
void (*func)(struct event *);
} event;
The last line inside the struct is supposed to be a pointer to a function with return type void with pointer to an event as an argument such as:
void function(event *evt);
Though, I get the following warning message: "its scope is only this definition or declaration, which is probably not what you want". Is this right or wrong?
A structure containing a pointer to itself is not a problem. A pointer has a fixed size, so it doesn't matter how big the size of the structure it points to is. On most systems you're likely to come across, a pointer will be either 4 bytes or 8 bytes in size.
Pointers to structures Although a structure cannot contain an instance of its own type, it can can contain a pointer to another structure of its own type, or even to itself. This is because a pointer to a structure is not itself a structure, but merely a variable that holds the address of a structure.
A structure T cannot contain itself.
Your struct needs needs to be defined like this:
typedef struct event // <<< note the `event` tag here
{
lamp *lamp;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
void (*func)(struct event *);
} event; // <<< you can still keep `event` as a typedef
// which is equivalent to `struct event`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With