Code

#include <stdio.h>
#include <stddef.h>

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

void print_offsets() {
MyStruct p;
printf("Offset of a: %lu\n", sizeof(p.a)-1);
int a = sizeof(a);
int b = sizeof(b);
int c = sizeof(c);
while(b<a && b%4 != 0)
{
    b = b+b;
}
printf("Offset of b: %lu\n", b);

while(c<b+a && c%8 != 0)
{
    c = c+c;
} 
printf("Offset of c: %lu\n", c);

printf("Size: %lu", sizeof(p));
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

Expected Output

Offset of a: 0 Offset of b: 4 Offset of c: 8 Size: 12