Code

#include <stdio.h>

typedef struct {
    char a; //4 bytes
    int b; //4 bytes
    short c; //2 bytes
} MyStruct;

void print_offsets() {
    MyStruct myStruct;
    myStruct.a = '0';
    myStruct.b = 4;
    myStruct.c = 8;
    printf("Offset of a: %c\n", myStruct.a);
    printf("Offset of b: %d\n", myStruct.b);
    printf("Offset of c: %hd\n", myStruct.c);
    printf("Size: %lu", sizeof(MyStruct));
}

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