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;

    long offset_a = (char*)&s.a - (char*)&s;
    long offset_b = (char*)&s.b - (char*)&s;
    long offset_c = (char*)&s.c - (char*)&s;
    long total_Size = sizeof(s);

    printf("Offset of a: %d\n", offset_a);
    printf("Offset of b: %d\n", offset_b);
    printf("Offset of c: %d\n", offset_c);
    printf("Size: %d", total_Size);

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote