Skip to content

Instantly share code, notes, and snippets.

@jeffs

jeffs/main.c Secret

Last active July 26, 2021 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffs/a14072313400f3eb9753ff2f6b76bbf6 to your computer and use it in GitHub Desktop.
Save jeffs/a14072313400f3eb9753ff2f6b76bbf6 to your computer and use it in GitHub Desktop.
nested-type-alignment-c
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
struct CStruct {
int16_t a; // 2 bytes
int32_t b; // 4 bytes
int16_t c; // 2 bytes
int32_t d; // 4 bytes
};
int main() {
puts("C struct:\n");
printf(" offset of a: %2ld bytes\n", offsetof(struct CStruct, a));
printf(" offset of b: %2ld bytes\n", offsetof(struct CStruct, b));
printf(" offset of c: %2ld bytes\n", offsetof(struct CStruct, c));
printf(" offset of d: %2ld bytes\n", offsetof(struct CStruct, d));
printf(" total size: %2ld bytes\n", sizeof(struct CStruct));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment