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;
    m.a='s';
    m.b=1;
    m.c=1;

    MyStruct *sptr= &m;

    char *aptr = &m.a;
    int *bptr = &m.b;
    short *cptr = &m.c;

    printf("Offset of a: %ld\n",(char*)aptr-(char*)sptr);
    printf("Offset of b: %ld\n",(char*)bptr-(char*)sptr);
    printf("Offset of c: %ld\n",(char*)cptr-(char*)sptr);
    printf("Size: %d",sizeof(m));
 
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote