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 s;
    uint8_t *p1, *p2, *p3, *p4;

    p1 = (uint8_t *) &s;
    p2 = (uint8_t *) &s.a;
    p3 = (uint8_t *) &s.b;
    p4 = (uint8_t *) &s.c;

    printf("Offset of a: %u\n", (p2 - p1));
    printf("Offset of b: %u\n", (p3 - p1));
    printf("Offset of c: %u\n", (p4 - p1));
    printf("Size: %u", sizeof(s));

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote