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 hey = {'A', 10, 20};
    size_t offset_a = (char*)&(hey.a)-(char*)&(hey);
    size_t offset_b = (char*)&(hey.b)-(char*)&hey;
    size_t offset_c = (char*)&(hey.c)-(char*)&hey;
    size_t mystruct = sizeof(hey);
    printf("Offset of a: %zu\n", offset_a);
    printf("Offset of b: %zu\n", offset_b);
    printf("Offset of c: %zu\n", offset_c);
    printf("Size: %zu", mystruct);
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote