Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct S;
    char *addr = (char*)&S;
    printf("Offset of a: %ld\n",((char*)&S.a - addr));
    printf("Offset of b: %ld\n", ((char*)&S.b - addr));
    printf("Offset of c: %ld\n",((char*)&S.c - addr));
    printf("Size: %zu", sizeof(S));
 
}

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