#include <stdio.h> typedef struct { char a; int b; short c; } MyStruct; void print_offsets() { // create an object MyStruct object; // Compute offsets using pointer arithmetic size_t offset_a = (size_t)((char*)&object.a - (char*)&object); size_t offset_b = (size_t)((char*)&object.b - (char*)&object); size_t offset_c = (size_t)((char*)&object.c - (char*)&object); // print offsets printf("Offset of a: %zu\n", offset_a); printf("Offset of b: %zu\n", offset_b); printf("Offset of c: %zu\n", offset_c); printf("Size: %zu\n", sizeof(MyStruct)); } 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