我試過在linux內核中使用container_of宏。container_of()宏中的賦值操作符如何工作
我所得到的由谷歌是遵循
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) \
({ const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type,member));})
#define CONT(prt, type, mem) container_of((prt), type, mem)
struct test {
int a;
};
struct m {
int b;
struct test t;
int c;
};
int main(int argc, char *argv[])
{
/* existing structure */
struct m *ma;
ma = malloc(sizeof(struct m));
ma->t.a = 5;
ma->b = 3;
/* pointer to existing entry */
struct test *te = &ma->t;
struct m *m = CONT(te, struct m, t);
printf("m->b = %d\n", m->b);
return EXIT_SUCCESS;
}
O/P M-> B = 3
,但我有在。我已經嘗試下面的* m的分配的疑問
#include <stdio.h>
int main()
{
int x = (int k = 9;k-2;);
printf("x= %d k = %d",x,k);
}
O/p
one.c:5: error: expected ‘)’ before ‘k’
one.c:5: error: expected expression before ‘;’ token
one.c:6: error: ‘k’ undeclared (first use in this function)
one.c:6: error: (Each undeclared identifier is reported only once
one.c:6: error: for each function it appears in.)
如果這個任務在這裏是錯誤的,那麼它是如何在宏的容器中工作的,這與以上兩者有什麼區別(我知道一個是宏而另一個是正常聲明,但是在宏擴展之後兩者看起來都是一樣的)
注意:不好意思,因爲這可能很簡單,但我無法弄清楚。
kinldy給我的dobut一些解決方案。
你覺得你想這個'INT X =做(INT K = 9; K-2);'? – 0decimal0
我想把值7分配給x。 – pradipta
不應該是這樣的:'int k = 9; int x = k-2;'? – 0decimal0