All submissions

Code

#include <stdio.h>

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

void print_offsets() {
     
    
    MyStruct test; 
    char *temp = (char*)&test;
    
    // printf("%p \n", &test); 
    // printf("%p \n", &test.a); 
    int totalSize=0; 
    long int temp1 = (char*)&test.a - temp;
    totalSize += temp1; 
    printf("Offset of a: %ld\n", temp1);
    
    temp1 = (char*)&test.b - temp; 
    totalSize += temp1;
    printf("Offset of b: %ld\n", temp1);

    temp1 = (char*)&test.c - temp; 
    totalSize += temp1;
    printf("Offset of c: %ld\n", temp1);

    printf("Size: %d\n", totalSize);

}

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

Solving Approach

 

 

 

Loading...

Input

Expected Output

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