container_of

#include <stdio.h>

struct foo {
    size_t a;
    size_t b;
    char c;
};

#define offsetof(type, member) \
    ((size_t)(&((type*)0)->member))

#define container_of(ptr, type, member) \
        ((type*)((char*)ptr - offsetof(type, member)))


int main()
{
    int a = 3000;
    a = ({ int a = 200; a; }); 

    struct foo s = { 
        .a = 100,
        .b = 200,
        .c = 10, 
    };  

    printf("%ld\n", container_of(&s.b, struct foo, b)->a);

    return 0;
}

你可能感兴趣的:(container_of)