How to access a structure member in a function that get it as void* type?
Posted
by Ahmad
on Stack Overflow
See other posts from Stack Overflow
or by Ahmad
Published on 2010-04-09T20:11:48Z
Indexed on
2010/04/09
20:13 UTC
Read the original article
Hit count: 258
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); }
© Stack Overflow or respective owner