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;

   char *ptr_s = (char*)&s;
   char* ptr_a = (char*)&s.a;
   char* ptr_b =(char*)&s.b;
   char* ptr_c =(char*)&s.c;


    printf("Offset of a: %ld",ptr_a - ptr_s);
    printf("\nOffset of b: %ld",ptr_b - ptr_s);
    printf("\nOffset of c: %ld",ptr_c - ptr_s);
    printf("\nSize: %zu",sizeof(s));
 
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote