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; 
   
    size_t offset_a = (size_t)((char*)&s.a - (char*)&s);
    size_t offset_b = (size_t)((char*)&s.b - (char*)&s);
    size_t offset_c = (size_t)((char*)&s.c - (char*)&s);
    size_t data_size = sizeof(s);
    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", data_size);
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote