Created
June 12, 2016 18:23
-
-
Save Alexey-N-Chernyshov/2835ee95908b681c8f5287b101cf4083 to your computer and use it in GitHub Desktop.
C struct allignment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stddef.h> | |
struct foo { | |
short a; | |
int b; | |
char c; | |
}; | |
int main(void) { | |
printf("sizeof(short) is %d\n", sizeof(short)); | |
printf("sizeof(int) is %d\n", sizeof(int)); | |
printf("sizeof(char) is %d\n\n", sizeof(char)); | |
printf("sizeof(foo) is %d\n\n", sizeof(struct foo)); | |
printf ("offsetof(struct foo,a) is %d\n",(int)offsetof(struct foo,a)); | |
printf ("offsetof(struct foo,b) is %d\n",(int)offsetof(struct foo,b)); | |
printf ("offsetof(struct foo,c) is %d\n",(int)offsetof(struct foo,c)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
sizeof(short) is 2
sizeof(int) is 4
sizeof(char) is 1
sizeof(foo) is 12
offsetof(struct foo,a) is 0
offsetof(struct foo,b) is 4
offsetof(struct foo,c) is 8