All submissions

Why does this raise cpp warnings??

#include <stdio.h>

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

void print_offsets() {
    padd ele;
    void *pele = &ele;
    void *pa = &(ele.a);
    void *pb = &(ele.b);
    void *pc = &(ele.c);
    int offa = (char*)pele - (char*)pa;
    printf("Offset of a: %d\n", offa);

    int offb = (char*)pele - (char*)pb;
    printf("Offset of b: %d\n", -offb);

    int offc = (char*)pele - (char*)pc;
    printf("Offset of c: %d\n", -offc);

    printf("Size: %d", (int)sizeof(ele));
}

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

Damn code doesnt let me use (void*) arithmetic as this uses cpp under the hood

 

 

 

Loading...

Input

Expected Output

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