I have 2 data structures as following:
struct data_type{
char name[20];
int (*check_equal)(void* a, void* b);
}
and
struct attr_data{
int data_type;
void* (*get_func)(void* record); //Not sure about prototype of this function
}
The first data structure is actually a structure used for different logical datatypes like string and int(c datatypes of short and int would come under "int" logical type). So, I have an array of this data_type struct with 2 elements in it: one with name as string and other one with name as integer. "check_equal" function for string is just a call to function strcmp() using args a and b given, which are typecasted to char*. Similar is case with the check_equal function for "integer" element. But notice here that in this implementation, the "check_equal" function would require pointers to 2 integers passed. The second data structure actually is a meta data for any other data structure like:
struct abc{
char* a;
int b;
}
For every member variable in struct abc, supposed I maintain an instance of struct attr_data in some array. Ideally a get function for some member variable should always return value of that member variable and not a pointer to that. Thus get function for member a in struct abc should return char* but that for b, should return an int. Now I need to use these get_funcs to directly pass an argument to the check_equal function of struct data_type. But since prototype of check_equal function is (void*,void*), using this get_func fails. Can anyone help me out deciding prototypes of function pointers used here? Can someone help understand the issues behind typecasting int to void*?
structwith aenumwhich hold the data type and aunionfor the actual data. Then pass this structure as argument and depending on the enum, perform the appropriate operation. That's actually how high level languages implement dynamic typing - z̫͋