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 result;
    int offset_a = (char*)&result.a - (char*)&result;
    int offset_b = (char*)&result.b - (char*)&result;
    int offset_c = (char*)&result.c - (char*)&result;
    int total_size = sizeof(result);


    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\n", total_size);

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote