Determine the type of a struct at runtime
Hello,
My data is coming in the form of a pointer to void. My aim is to dump the data to stderr in a structured way. The data pointed to by the struct is a struct that could be of several types. In order to print this data out in a structured way, there is the need to determine its type.
Code:
static void dump(const char *prefix, size_t size)
{
//cur_data is a global variable, we wish to print out the struct pointed to by it
}
My first thought is that we can use sizeof such that, if the size of the global variable 'cur_data' equal the size of the a certain struct, then we can assume that the contents of the global variable is the said struct.
Code:
size_t struct_size = sizeof(*cur_data);
switch(struct_size){
case sizeof(struct A):
case sizeof(struct B):
case sizeof(struct C);
default:
}
Could someone know of a better way to do this?