Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct s; 
   
    size_t offset_a = (size_t)((char*)&s.a - (char*)&s);
    size_t offset_b = (size_t)((char*)&s.b - (char*)&s);
    size_t offset_c = (size_t)((char*)&s.c - (char*)&s);
    size_t data_size = sizeof(s);
    printf("Offset of a: %zu\n", offset_a);
    printf("Offset of b: %zu\n", offset_b);
    printf("Offset of c: %zu\n", offset_c);
    printf("Size: %zu", data_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