Advertisement
Tobiahao

08_STRUKTURY_02

Jan 21st, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. /*
  2.  2. Zadeklaruj w programie unię zawierającą pola różnych typów. Następnie napisz funkcję, która wy- pisze rozmiar tej unii na ekranie, oraz wypisze sumę rozmiaru wszystkich jej pól. Jak wytłumaczyć zjawisko, że nie zawsze wartości te są równe?
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. union NewUnion{
  8.     int var1;
  9.     char var2;
  10.     double var3;
  11. } example_union;
  12.  
  13. void print_size(union NewUnion example_union)
  14. {
  15.     unsigned long sum_of_all_vars = 0;
  16.     sum_of_all_vars += sizeof(example_union.var1);
  17.     sum_of_all_vars += sizeof(example_union.var2);
  18.     sum_of_all_vars += sizeof(example_union.var3);
  19.    
  20.     printf("Size of all variables in union: %lu\n", sum_of_all_vars);
  21.     printf("Size of union %lu\n", sizeof(example_union));
  22. }
  23.  
  24. int main(void)
  25. {
  26.     print_size(example_union);
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement