Code

#include <stdio.h>

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

void print_offsets() {
 MyStruct s;
 
 int a = (char *)&s- &s.a;
 int b = (char *)&s.b - &s.a;
 int c = (char *)&s.c- &s.a;

 printf("Offset of a: %i\n",a);
 printf("Offset of b: %i\n",b);
 printf("Offset of c: %i\n",c);
 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