Having:
struct packet_sample_t {
int common_id;
int some;
char data[];
float foo;
}
Having the following function:
void dispatch_packet(void *packet);
My goal would be to parse the packet id and then call its handler, but I can't retrieve the struct variable common_id
from void *
.
I would like to create something like an interface
in hight level languages, assuming that all my packets structures should have the variable common_id
.
So I'm looking something that would work like below:
struct packet_base {
int common_id;
}
void dispatch_packet(void *packet) {
int common_id = ( (packet_base *)packet )->common_id;
switch(common_id) {...}
}
void test() {
packet_sample_t packet = {.common_id = 10, ...};
dispatch_packet((void *) &packet); //this function should retrieve `common_id`
packet_other_t other = {.common_id = 1};
dispatch_packet((void *) &other); // again with another packet
}
Im not that familiar to C language and I dont really know how I could do this. But in simple words, I would like to be able to cast a packet to its packet_base, that are sharing both a common variable.
EDIT: more details in the example