24小时热门版块排行榜    

查看: 350  |  回复: 1

sudo

木虫 (正式写手)

[交流] 版面冷清,放一个阅读理解题吧 已有1人参与

欢迎大家来随意吐槽......
CODE:
#include                                                          

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

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

typedef int BaseType;

struct T{
        BaseType base;
        void (*show)();
};

void show1(){
        printf("This is 1.\n");
}

void show2(){
        printf("This is 2.\n");
}

void Tshow(BaseType* base){
        container_of(base, struct T, base)->show();
}

int main(){
        struct T a, b;
       
        BaseType *pa, *pb;

        a.base = 1; a.show = show1;
        b.base = 2; b.show = show2;

        pa = &a.base;
        pb = &b.base;

        Tshow(pa);
        Tshow(pb);

        return 0;
}

【题一】简述offsetof和container_of两个宏的作用
【题二】原版的container_of宏是这样的(有一些gcc的扩展语法):
CODE:
#define container_of(ptr, type, mem) ( { \
        const typeof( ((type*)0)->mem )* __ptr = (ptr);\
        (type *)( (char*)__ptr - offsetof(type,mem)); \
        })

请问多出来的这句const typeof( ((type*)0)->mem )* __ptr = (ptr);是什么用意?
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lijie169

铜虫 (著名写手)


小木虫: 金币+0.5, 给个红包,谢谢回帖
CODE:
(size_t) &( (type *)0 )->mem

这是神马意思 好好解释一下那个0
2楼2012-04-28 11:02:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 sudo 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见