#include <stdio.h> typedef struct { char a; int b; short c; } MyStruct; void print_offsets() { MyStruct *s; unsigned long base_addr = (unsigned long)s; printf("Offset of a: %lu\n", (unsigned long)&(s->a) - base_addr); printf("Offset of b: %lu\n", (unsigned long)&(s->b) - base_addr); printf("Offset of c: %lu\n", (unsigned long)&(s->c) - base_addr); printf("Size: %d",sizeof(*s)); } 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