Advertisement
Guest User

offsetof() theory and practice header stddef.h

a guest
Aug 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stddef.h>
  3. struct estructura{
  4.     int memberinstructure; //0
  5.     char array[20]; //0 + sizeof(int) = 0 + 4    "4 because sizeof(int) = 4 in memberinstructure"
  6.     int end; //0 + sizeof(int) + (20 * sizeof(char)) = 0 + 4 + (20 * 1) "20 because sizeof(char) = 1 and array = 20"     "20 * 1 = 20"
  7.     //But the offsetof never give total size from struct.
  8.     //Total size = 0 + 4 + (20 * 1) + 4 = 28 "The ultimate 4 because int end == sizeof(int) = 4"
  9. };
  10. void main(){
  11.     printf("Size: %d\n",offsetof(struct estructura,end));
  12.     //offsetof() theory--------------------------------------------
  13.     int operation1 = 0 + sizeof(int);
  14.     int operation2 = operation1 + (20 * sizeof(char));
  15.     //-------------------------------------------------------------
  16.     printf("offsetof() Theory Operation =  %d\n",operation2);
  17.     printf("Is %d == %d ??",offsetof(struct estructura,end),operation2);
  18.     if (offsetof(struct estructura,end) == operation2){
  19.         printf("True\n\n");
  20.     }
  21.     //-------------------------------------------------------------
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement