fork(1) download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. enum { is_int, is_float, is_char, is_pointer } type;
  5. union {
  6. int i;
  7. float f;
  8. char c;
  9. void *p;
  10. } value;
  11. } Tipo;
  12.  
  13. int main(void) {
  14. int x = 10;
  15. float y = 5.5f;
  16. char c = 'h';
  17. char a[] = "teste";
  18. Tipo var1 = { .type = is_int, .value.i = x };
  19. Tipo var2 = { .type = is_float, .value.f = y };
  20. Tipo var3 = { .type = is_char, .value.c = c };
  21. Tipo var4 = { .type = is_pointer, .value.p = a };
  22. printf("%d\n", var1.value.i);
  23. printf("%f\n", var2.value.f);
  24. printf("%c\n", var3.value.c);
  25. printf("%s\n", (char *)var4.value.p);
  26. printf("%d\n", var2.type);
  27. printf("%zd\n", sizeof(var2));
  28. printf("%d\n", var3.type);
  29. printf("%zd\n", sizeof(var3));
  30. }
  31.  
  32. //http://pt.stackoverflow.com/q/180783/101
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
10
5.500000
h
teste
1
16
2
16