Code

#include <stdio.h>

typedef struct {
    char a;
    int b;
    short c;
} MyStruct;

void print_offsets() {
    MyStruct ms;
    char offset_a = 0;
    char padding_a = sizeof(ms.b) - sizeof(ms.a);
    char offset_b = offset_a + sizeof(ms.a) + padding_a;
    char offset_c = offset_b + sizeof(ms.b); 
    char padding_c = sizeof(ms.b) - sizeof(ms.c);

    char sz = offset_c + padding_c + sizeof(ms.c);

    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", sz);
}

int main() {
    print_offsets();
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

Expected Output

Offset of a: 0 Offset of b: 4 Offset of c: 8 Size: 12