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