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 stct;
    char *p3, *p2, *p1, *p0;

    p0 = (char *) &stct;
    p1 = (char *) &stct.a;
    p2 = (char *) &stct.b;
    p3 = (char *) &stct.c;

    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", sizeof(stct));
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote