All submissions

Code

#include <stdio.h>

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

void print_offsets() {
   MyStruct s;
    
   
    char* base = (char*)&s;

    
    size_t offset_a = 0;

    
    size_t offset_b = (size_t)((char*)&s.b - base);

    // Offset of 'c' is the difference between the address of 'c' and the base address
    size_t offset_c = (size_t)((char*)&s.c - base);

    // 3. Compute the total size of the struct
    size_t total_size = sizeof(MyStruct);
    
    // 4. Print the results
    printf("Offset of a: %u\n", offset_a);
    printf("Offset of b: %u\n", offset_b);
    printf("Offset of c: %u\n", offset_c);
    printf("Size: %u \n", total_size);

}

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