2010-07-14 55 views

回答

2

事情是這樣的:

struct list_head head; /* previously initialized */ 
struct list_head *pos; 

list_for_each(pos, head) 
{ 
    your_type *elt; 
    elt = list_entry(pos, typeof(*elt), name_of_list_head_struct_member); 
    /* and print *elt! */ 
} 
+0

注意:這項工作的前提條件是您將'your_type'定義爲'list_head'作爲其第一個成員。這可以確保'list_head'的地址與'you_type'結構的地址相同。 – torak 2010-07-14 15:18:04

+1

@torak:這是不正確的,它與list_head成員的位置無關,並且可以有多個list_head結構。 這個工作的原因是'list_entry'使用'container_of'宏從結構成員(這裏是'name_of_list_head_struct_member')獲得正確的偏移量 – Hasturkun 2010-07-14 16:17:40