Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stddef.h>
- struct estructura{
- int memberinstructure; //0
- char array[20]; //0 + sizeof(int) = 0 + 4 "4 because sizeof(int) = 4 in memberinstructure"
- int end; //0 + sizeof(int) + (20 * sizeof(char)) = 0 + 4 + (20 * 1) "20 because sizeof(char) = 1 and array = 20" "20 * 1 = 20"
- //But the offsetof never give total size from struct.
- //Total size = 0 + 4 + (20 * 1) + 4 = 28 "The ultimate 4 because int end == sizeof(int) = 4"
- };
- void main(){
- printf("Size: %d\n",offsetof(struct estructura,end));
- //offsetof() theory--------------------------------------------
- int operation1 = 0 + sizeof(int);
- int operation2 = operation1 + (20 * sizeof(char));
- //-------------------------------------------------------------
- printf("offsetof() Theory Operation = %d\n",operation2);
- printf("Is %d == %d ??",offsetof(struct estructura,end),operation2);
- if (offsetof(struct estructura,end) == operation2){
- printf("True\n\n");
- }
- //-------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement