#include <stdio.h>
#include <stdint.h>
typedef struct {
char a;
int b;
short c;
} MyStruct;
void print_offsets() {
//Declare a variable of this struct
MyStruct s;
//Declare pointers to each member of the struct and as a whole to the struct
uint8_t *ps, *pa, *pb, *pc;
ps = (uint8_t *) &s;
pa = (uint8_t *) &s.a;
pb = (uint8_t *) &s.b;
pc = (uint8_t *) &s.c;
printf("Offset of a: %u\n", (pa - ps));
printf("Offset of b: %u\n", (pb - ps));
printf("Offset of c: %u\n", (pc - ps));
printf("Size: %u", sizeof(s));
}
int main() {
print_offsets();
return 0;
}
Loading...
Input
Expected Output
Offset of a: 0
Offset of b: 4
Offset of c: 8
Size: 12