#include <stdio.h> typedef struct { char a; int b; short c; } MyStruct; MyStruct obj; void print_offsets() { printf("Offset of a: %d\n",(size_t)((char*)&obj.a - (char*)&obj)); printf("Offset of b: %d\n",(size_t)((char*)&obj.b - (char*)&obj)); printf("Offset of c: %d\n",(size_t)((char*)&obj.c - (char*)&obj)); printf("Size: %d",(size_t)((char*)&obj.a - (char*)&obj)+(size_t)((char*)&obj.b - (char*)&obj)+(size_t)((char*)&obj.c - (char*)&obj)); } int main() { print_offsets(); return 0; }
Test Cases
Test Results
Input
Expected Output
Offset of a: 0 Offset of b: 4 Offset of c: 8 Size: 12