Code

#include <stdio.h>
#include <stdint.h>

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

void print_offsets() {
    MyStruct res={0};
    uint8_t * p0 = (uint8_t *)(&res);
    uint8_t * p1 = (uint8_t *)(&(res.a));
    uint8_t * p2 = (uint8_t *)(&(res.b));
    uint8_t * p3 = (uint8_t *)(&(res.c));
    uint8_t total = sizeof(res);
    // print values
    printf("Offset of a: %u\n",p1-p0);
    printf("Offset of b: %u\n",p2-p0);
    printf("Offset of c: %u\n",p3-p0);
    printf("Size: %u\n",total);
}

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