78. Struct Padding

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>

typedef struct {
    char a;
    int b;
    short c;
} MyStruct;

void print_offsets() {
    MyStruct t;
    int boff = (uint8_t*)&t.b - (uint8_t*)&t;
    int coff = (uint8_t*)&t.c - (uint8_t*)&t;
    int size = sizeof(t);
    printf("Offset of a: 0\nOffset of b: %d\nOffset of c: %d\nSize: %d", boff, coff, size);
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote