1 /**
2 *teakey
3 *2012-07-25
4 */
5 #include<stdio.h>
6 #include<stdlib.h>
7 #define list_entry(ptr,type,member) \
8 (type *)( (char *)(ptr) - (unsigned long)(&(((type*)0)->member)))
9
10 struct list_head
11 {
12 struct list_head *next;
13 struct list_head *prev;
14 };
15 typedef struct person
16 {
17 int age;
18 struct list_head list;
19 }Person;
20
21 int main(int argc,char **argv)
22
23 {
24 Person person;
25 person.age=25;
26 struct list_head list;
27 list.next=&list;
28 list.prev=&list;
29 person.list=list;
30 Person *pos=list_entry(&person.list,Person,list);
31 printf("the age of person is %d",pos->age);
32 return 0;
33
34 }
上面的例子应该不用讲太多把,相信你自己哦。。。
关键是linux内核链表的实现,这个很有实际应用价值。list_entry,简直就是太棒了。