Advertisement
Tobiahao

08_STRUKTURY_01

Jan 21st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. /*
  2.  1. Zadeklaruj w programie strukturę zawierającą pola różnych typów. Następnie napisz funkcję, która wypisze rozmiar tej struktury 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. struct NewStructure{
  8.     int var1;
  9.     char var2;
  10.     double var3;
  11. } example_struct;
  12.  
  13. void print_size(struct NewStructure example_struct)
  14. {
  15.     unsigned long sum_of_all_vars = 0;
  16.     sum_of_all_vars += sizeof(example_struct.var1);
  17.     sum_of_all_vars += sizeof(example_struct.var2);
  18.     sum_of_all_vars += sizeof(example_struct.var3);
  19.    
  20.     printf("Size of all variables in structure: %lu\n", sum_of_all_vars);
  21.     printf("Size of structure %lu\n", sizeof(example_struct));
  22. }
  23.  
  24. int main(void)
  25. {
  26.     print_size(example_struct);
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement