I created a simple list structure in C that contains any data using the void * pointer, for example:
struct node
{
void *the_data;
node *next;
};
It works fine, but I have a list containing, for example, only struct_a in the list and only struct_b in another list. I need to check if the first element of the list has type struct_a or type struct_b in one if () question so that I know the data stored in this list. How to do it? Thanks
Edit:
I came up with a simple solution that is enough for the problem now, I don’t know if this is better:
struct node
{
void *data;
node *next;
}
And I added a “handle” to the node, which contains the address of the first node and type information:
struct list_desc
{
node *list;
short int type;
}
and I will use it with some macros like
#define __data_type1 10
#define __data_type2 20
so later I can compare like if (struct.type == __data_tipe1) etc.
source