c - Is it possible to pass a variable as a structure member? -
if have struct this:
/* defined structure 3 integer members */ typedef struct mystruct mystruct; struct myscruct{ int x; int y; int z; }; int main() { mystruct example; // declared "example" type "mystuct" example.x=1; // member 1 have value 1 example.y=2; // member 2 have value 2 example.z=3; // member 3 have value 3 char c; printf("\nchoose witch member want print [ x, y or z]"); scanf("%c", &c); while(getchar() != '\n'); printf("%d", example.c); // part don't know possible. return 0; }
it possible pass different member last printf instead of use combination of if example? if so, what's syntax of it?
no, cannot use runtime data reference symbols source code. can accomplish apparent aim of particular code bit more effort, though:
typedef struct mystruct mystruct; struct myscruct{ int x; int y; int z; }; int main() { mystruct example; // declared "example" type "mystuct" example.x=1; // member 1 have value 1 example.y=2; // member 2 have value 2 example.z=3; // member 3 have value 3 char c; int xyz; printf("\nchoose witch membeer want print [ x, y or z]"); scanf("%c", &c); switch (c) { case 'x': xyz = example.x; break; case 'y': xyz = example.y; break; case 'z': xyz = example.z; break; default: puts("oops\n"); return 1; } printf("%d", xyz); return 0; }
there other alternatives, too, none direct attempting.
Comments
Post a Comment