[STM8] Error[Pe029]: expected an expression 知道原因不

[复制链接]
 楼主| jd9945 发表于 2016-7-19 16:24 | 显示全部楼层 |阅读模式

#define offsetof(TYPE, MEMBER) ((unsigned int) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) );})


struct student {
        char name[20];
        char sex;
};



void   main(void)
{   
  
//  off_set = offsetof(student_info, age);  
//  stu_info.age = 10;
//  ptr = container_of(&stu_info.age, student_info, age);
    struct student st={"zhangsan",'m'};
    struct student *tmp;
    char *ptr_name=st.name;
    tmp=container_of(ptr_name,struct student,name);
}
 楼主| jd9945 发表于 2016-7-19 16:25 | 显示全部楼层
在iar中编译不行, 换成mdk也不行
JasonWangSE 发表于 2016-7-19 17:08 | 显示全部楼层
你说的是 typeof吗?换成gcc估计就行了
 楼主| jd9945 发表于 2016-7-19 17:21 | 显示全部楼层
是啊, 在gcc中可以,想移植到mcu中去
JasonWangSE 发表于 2016-7-19 17:30 | 显示全部楼层
本帖最后由 JasonWangSE 于 2016-7-19 17:42 编辑
jd9945 发表于 2016-7-19 17:21
是啊, 在gcc中可以,想移植到mcu中去

typeof是gcc的扩展关键字,需要编译器支持;其实强转也能实现
#define container_of(ptr, type, member) \
            (type *)((long)ptr - offsetof(type, member))



 楼主| jd9945 发表于 2016-7-19 20:18 | 显示全部楼层
谢谢, 我试试看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

84

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

26

主题

84

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部