Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct res;
    MyStruct *ptr = &res;
    
  
 
    printf("Offset of a: %lu\n" ,ptr - ptr);
    printf("Offset of b: %d\n" ,(ptr + 4) - ptr );
    printf("Offset of c: %lu\n" ,(ptr + 8) - ptr );
   
    printf("Size: %lu" ,sizeof(*ptr) );


 
}

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