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 m;
     printf("Offset of a: %lu\n", ((char*)(&m.a)-(char*)(&m)));
     printf("Offset of b: %lu\n", ((char*)(&m.b)-(char*)(&m)));
     printf("Offset of c: %lu\n", ((char*)(&m.c)-(char*)(&m)));
     printf("Size: %lu",sizeof(m));
     

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote