you can't extract the name of the struct in a smart and clean way.
the possibility to call the type of a variable or a struct is called "Reflection" - the application ability to reflect upon itself (either in compile or runtime) and extract data like the type, typename, inner variables etc.
this is not supported at all in C. thinking about it , C doesn't care- a struct is basically a row in the memory (like any other variable in C, actually).
you can however , make a not-so-smart implementation that stores the typename equivilant to the memory address :
struct memory_address_to_type_name{
char type_name [20],
void* memory_address
} name_memory_address_map[50];
char* get_name (void* variable){
int i;
for (i=0;i<50;i++){
if (variable == name_memory_address_map[i].memory_address)){
return name_memory_address_map[i].type_name;
}
}
return "not found";
}
int push_name (void* variable , char* type_name){
int i;
for (i=0;i<50;i++){
if (strcmp(name_memory_address_map[i].type_name,"") == 0){
name_memory_address_map[i].memory_address = variable;
strcpy(name_memory_address_map[i].type_name,type_name);
}
return 1;
}
return 0;
}
}
int main (void){
myStruct x;
push_name (&x,"myStruct");
//other code
printf("%s --> %d",get_name(x),x.my_member);
}
of course , this is not a complete example. you do want to use a linked list instead of a ad-hoc bounded array , do much more protecting against overflows and out of arrays errors , etc. this is only the idea.
as a side note (and I might get downvoted for this), as a C++ developer, your problem could be much more easily solved in C++ by using a typeid(x).name()
(if the implementation does return normal string , like VC++ implementation) , or reproduce the solution above with std::map and std::string . but this is a side note only.