亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4311 | 回復(fù): 7
打印 上一主題 下一主題

[數(shù)據(jù)結(jié)構(gòu)] 宏container_of用法問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-09-04 15:39 |只看該作者 |倒序瀏覽
  1. #include <stdio.h>

  2. #define FIND(TYPE,MEMBER) (size_t)&(((TYPE*)0)->MEMBER)

  3. #define container_of(ptr, type, member) ({                      \
  4.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  5.         (type *)( (char *)__mptr - offsetof(type,member) );})

  6. struct list_head {
  7.         struct list_head *next, *prev;
  8. };

  9. struct student
  10. {
  11.         int a;
  12.         char b[19];
  13.         double c;
  14.         struct list_head list;
  15. };

  16. int main(void)
  17. {       
  18.         struct student s= {1,{0},0.0,{NULL,NULL}};
  19.         struct list_head * ptr = &s.list;

  20.         printf("%08x\n",s);
  21.         printf("%08x\n",&s);
  22.         printf("%08x\n",&s.a);
  23.         printf("%08x\n",&s.b);
  24.         printf("%08x\n",&s.c);
  25.         printf("%08x\n",FIND(struct student,c));
  26.         struct student *stu  = container_of(ptr,struct student,s.list);//這一句上有問題。
  27.         printf("%08x\n",stu);
  28.         return 0;
  29. }
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2012-09-04 16:06 |只看該作者
  1. #include <stdio.h>

  2. #define FIND(TYPE,MEMBER) (size_t)&(((TYPE*)0)->MEMBER)

  3. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

  4. #define container_of(ptr, type, member) ({                      \
  5.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  6.         (type *)( (char *)__mptr - offsetof(type,member) );})

  7. struct list_head {
  8.     struct list_head *next, *prev;
  9. };

  10. struct student
  11. {
  12.     int a;
  13.     char b[19];
  14.     double c;
  15.     struct list_head list;
  16. };

  17. int main(void)
  18. {        
  19.     struct student s= {1,{0},0.0,{NULL,NULL}};
  20.     struct list_head * ptr = &s.list;

  21.     printf("%08x\n",s);
  22.     printf("%08x\n",&s);
  23.     printf("%08x\n",&s.a);
  24.     printf("%08x\n",&s.b);
  25.     printf("%08x\n",&s.c);
  26.     printf("%08x\n",FIND(struct student,c));
  27.     struct student *stu  = container_of(ptr,struct student, list);//這一句上有問題。
  28.     printf("%08x\n",stu->a);
  29.     return 0;
  30. }
復(fù)制代碼
你忘記聲明 container_of 中的 offsetof 宏了.

而且在使用 container_of 的時候, 也有錯誤. 第三個參數(shù), 是對象在結(jié)構(gòu)體中的名稱.

論壇徽章:
0
3 [報告]
發(fā)表于 2012-09-04 16:18 |只看該作者
你試了沒呀,我在vc6上試還是編譯不通回復(fù) 2# hk2305621


   

論壇徽章:
0
4 [報告]
發(fā)表于 2012-09-04 16:31 |只看該作者
我在 linux 下用 gcc 編譯的.  是 ok 的.

VC 6 好像有自己的 offset 函數(shù).
  1. $ gcc container.c
  2. container.c: In function ‘main’:
  3. container.c:28: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘struct student’
  4. container.c:29: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘struct student *’
  5. container.c:30: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘int *’
  6. container.c:31: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘char (*)[19]’
  7. container.c:32: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘double *’
  8. $ ./a.out
  9. 00000001
  10. bfc2b6e4
  11. bfc2b6e4
  12. bfc2b6e8
  13. bfc2b6fc
  14. 00000018
  15. 00000001
復(fù)制代碼

論壇徽章:
0
5 [報告]
發(fā)表于 2012-09-04 16:49 |只看該作者
非常感謝。。〗K于等到了。呵呵,能加個qq嗎?我qq:718268255.回復(fù) 4# hk2305621


   

論壇徽章:
0
6 [報告]
發(fā)表于 2012-09-04 17:25 |只看該作者
回頭找你. 在公司, 不能上Q. 我的Q. 394831530.

論壇徽章:
0
7 [報告]
發(fā)表于 2012-09-05 15:32 |只看該作者
樓主高手,把Linux程序移植到windows

論壇徽章:
0
8 [報告]
發(fā)表于 2012-09-06 13:58 |只看該作者
樓主問題解決了咋不來公布下結(jié)果呢?
linux的鏈表確實是個不錯的設(shè)計,把它移植到windows應(yīng)該很多人都干過吧,我以前也做過。
VC6確實有自己的offsetof,我在VC下的container_of是這么寫的:
  1. #define container_of(ptr, type, member)                \
  2.         (type *)((char *)ptr - offsetof(type, member))
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP