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 temp;
 printf("Offset of a: %d\n",(uint8_t *)(&temp) - (uint8_t *)(&temp.a));
  printf("Offset of b: %d\n",(uint8_t *)(&temp.b) - (uint8_t *)(&temp));
   printf("Offset of c: %d\n",(uint8_t *)(&temp.c) - (uint8_t *)(&temp));
    printf("Size: %d",sizeof(temp));
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote