Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct my_struct;
    int max = sizeof(my_struct.a) > sizeof(my_struct.b) ? sizeof(my_struct.a) : sizeof(my_struct.b);
    max = max > sizeof(my_struct.c) ? max : sizeof(my_struct.c);

    int offset = 0;
    printf("Offset of a: %zu\n"
            "Offset of b: %zu\n"
            "Offset of c: %zu\n"
            "Size: %zu", 
            offset, offset + max, offset + 2*max, sizeof(my_struct));
}

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