一个以前的问题 但是与此事无关:https://www.eefocus.com/xiao2030/blog/17-02/404744_5efcd.html

 

怎么办???PC SUBLIME测试可以的!

#include <stdio.h>
#include <string.h>//memset
#include <stdlib.h>//free
#include <stddef.h>
#define uint8_t  unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int


// 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

// 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

struct student
{
    char gender;
    int id;
    int age;
    char name[20];
};

void show(struct student *pstu)
{
	printf("gender= %c\n", pstu->gender);
	printf("id= %d\n", pstu->id);
	printf("age= %d\n", pstu->age);
	printf("name= %s\n", pstu->name);
}
int main()
{
    struct student stu;
    struct student *pstu;

    stu.gender = '1';
    stu.id = 123;
    stu.age = 24;
    strcpy(stu.name, "zhouxingxing");

show(&stu);
#if 1
    // 根据"id地址" 获取 "结构体的地址"。
    pstu = container_of(&stu.id, struct student, id);
    // 根据获取到的结构体student的地址,访问其它成员
#else
 	pstu = &stu;
#endif

 	show(pstu);
    return 1;
}

#if 01 都可以成功赋值!

 

 

问题定位:

    pstu = container_of(&stu.id, struct student, id);

这个句子 在PC是可以 在KEIL不可以!!!!

..\GMODES\list\Glist.c(119): error:  #29: expected an expression

 

自己分析

pstu = container_of(&stu.id, struct student, id);


pstu =     
({          
const typeof( ((struct student *)0)->id ) *__mptr = (&stu.id);    
(struct student *)( (char *)__mptr - offsetof(struct student,id) );
});

其中: 
offsetof(struct student,id)  OK
(struct student *)( (char *)__mptr - offsetof(struct student,id) ) OK

问题定位:const typeof( ((struct student *)0)->id ) *__mptr = (&stu.id)
typeof 没有定义!
 

怎么办?

 

插入一下 自己测试一次( {})的运算符

#include <stdio.h>
#include <string.h>//memset
#include <stdlib.h>//free
#include <stddef.h>
#define uint8_t  unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int


// 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。
//#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

// 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

struct student
{
    char gender;
    int id;
    int age;
    char name[20];
};

void show(struct student *pstu)
{
	printf("gender= %c\n", pstu->gender);
	printf("id= %d\n", pstu->id);
	printf("age= %d\n", pstu->age);
	printf("name= %s\n", pstu->name);
}
int main()
{
    struct student stu;
    struct student *pstu;

    stu.gender = '1';
    stu.id = 123;
    stu.age = 24;
    strcpy(stu.name, "zhouxingxing");

show(&stu);
#if 1
    // 根据"id地址" 获取 "结构体的地址"。
    pstu =({1,2,container_of(&stu.id, struct student, id);}) ;
    // 根据获取到的结构体student的地址,访问其它成员
#else
 	pstu = &stu;
#endif

 	show(pstu);
    return 1;
}

正常工作!!!!!!!

现在是

#define gcontainer_of(ptr, type, member) ({          \
(type *)( (char *)ptr - goffsetof(type,member) );})

不行


#define gcontainer_of(ptr, type, member)           \
(type *)( (char *)ptr - goffsetof(type,member) )
可以

 

 

此时结论:

KEIL

// 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。
#define goffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)  

pstu =  ( struct student *)( (char *)&stu.id - goffsetof(struct student,id) );-------------------OK
pstu =  ({ ( struct student *)( (char *)&stu.id - goffsetof(struct student,id) ) ;});: error:  #29: expected an expression

 

问题现场保留:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include "glist.h" 

struct person 
{ 
    int age; 
    char name[20];
    struct list_head list; 
};


// 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。
#define goffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

// 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针
#define gcontainer_of(ptr, type, member)   ({  (type *)( (char *)ptr - goffsetof(type,member) );})

struct student
{
    char gender;
    int id;
    int age;
    char name[20];
};

void show(struct student *pstu)
{
	printf("gender= %c\n", pstu->gender);
	printf("id= %d\n", pstu->id);
	printf("age= %d\n", pstu->age);
	printf("name= %s\n", pstu->name);
}
int testonly(void)
{
    struct student stu;
    struct student *pstu;

    stu.gender = '1';
    stu.id = 123;
    stu.age = 24;
    strcpy(stu.name, "zhouxingxing");

show(&stu);
#if 1
   //pstu =  ( struct student *)( (char *)&stu.id - goffsetof(struct student,id) );
   pstu =  ({ (( struct student *)( (char *)&stu.id - goffsetof(struct student,id) )) ;});
#else
 	pstu = &stu;
#endif

 	show(pstu);
    return 1;
}

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐