fork download
  1. #include <stdio.h>
  2.  
  3. struct structA_t{
  4. char* a_one;
  5. char* a_two;
  6. };
  7.  
  8. struct structB_t{
  9. char* b_one;
  10. char* b_two;
  11. char* b_three;
  12. };
  13.  
  14. struct structC_t{
  15. char* c_one;
  16. char* c_two;
  17. char* c_three;
  18. };
  19.  
  20. typedef enum my_e_dataId{
  21. dataid_invalid = 0,
  22. dataid_a,
  23. dataid_b,
  24. dataid_c
  25. } my_dataId;
  26.  
  27. typedef union u_data {
  28. struct structA_t* a;
  29. struct structB_t* b;
  30. struct structC_t* c;
  31. }mydata;
  32.  
  33. typedef struct s_some_type{
  34. my_dataId dataId;
  35. mydata myData;
  36. }some_type;
  37.  
  38. static struct structA_t a = {"ads", "as"};
  39. static struct structB_t b = {"zzds", "dfr", "shywsd"};
  40. static struct structC_t c = {"ssa", "ad", "dhksdhs"};
  41.  
  42. int main (int argc, char** argv){
  43. some_type sta[] = {
  44. {dataid_a, (struct structA_t*) &a},
  45. {dataid_b, (struct structA_t*) &b},
  46. {dataid_c, (struct structA_t*) &c}
  47. };
  48.  
  49. printf("second field of a: %s\n", sta[0].myData.a->a_two);
  50. printf("third field of c: %s\n", sta[2].myData.c->c_three);
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
second field of a: as
third field of c: dhksdhs