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 s;
 int OffA = 0,offb=0, offc = 0;
 offb = (char *)&s.b-&s.a;
 offc = (char *)&s.c-&s.a;
 printf("Offset of a: 0\nOffset of b: %d\nOffset of c: %d\nSize: %d",offb, offc,(offb+offc));
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote