How to access a structure member in a function that get it as void* type?
- by Ahmad
I want to have a function that accepts different type of structures as argument.
So, since I don't have a specific type, I have to use void*. Now question is: when I pass a structure to this function, how can I access a known member of this structure inside the function? Specifically, I know that all structures have str1 as a member and I want, for example, print it.
Here is a sample code:
struct {
char* str1;
float tt1; } var1 = {"This is me", 12};
struct {
char* str1;
int tt2; } var2 = {"This is you", 18};
void printStruct(void* str)
{
printf("\n the structure string is %s", ??);
//can I put something in ?? to print the string?
}
main(....)
{
printStruct(&var1);
printStruct(&var2);
}