Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct size;
    int Offset  = 0;
    int a_size = sizeof(size.a);
    int b_size = sizeof(size.b);
    int c_size = sizeof(size.c);
    if (a_size > 4) {
        Offset = a_size + a_size % 4;
    }
    printf("Offset of a: %d\n",Offset);
    Offset+=4;
    if (b_size > 4) {
        Offset += b_size + b_size % 4;
    }
    printf("Offset of b: %d\n", Offset); 
    Offset+=4;      
    if (c_size > 4) {
        Offset += c_size + c_size % 4;
    }
    printf("Offset of c: %d\n", Offset);
    Offset+=4;   
    printf("Size: %d\n", sizeof(size));    
}

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