78. Struct Padding

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

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

void print_offsets() {
    MyStruct st = {0}; 
    long stBase = (long) &st;

    int offset_a = (long) &(st.a) - stBase;
    int offset_b = (long) &(st.b) - stBase;
    int offset_c = (long) &(st.c) - stBase;

    printf("Offset of a: %d\n", offset_a);
    printf("Offset of b: %d\n", offset_b);
    printf("Offset of c: %d\n", offset_c);

    printf("Size: %d", sizeof(st));
}

int main() {
    print_offsets();
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote