#include <stdio.h> #include <stdint.h> typedef struct{ char a; int b; short c; }MyStruct; void printf_offset(MyStruct tmp){ char *add_base = (char*)&tmp; int offset_a = (char*)&tmp.a - add_base; int offset_b = (char*)&tmp.b - add_base; int offset_c = (char*)&tmp.c - add_base; printf("Offset of a: %d\n",offset_a); printf("Offset of b: %d\n",offset_b); printf("Offset of c: %d\n",offset_c); printf("Size: %d\n",sizeof(MyStruct)); } int main(){ MyStruct str1; printf_offset(str1); return 0; }
Test Cases
Test Results
Input
Expected Output
Offset of a: 0 Offset of b: 4 Offset of c: 8 Size: 12