views:

93

answers:

1

How can I print the list values using list.h defined in /include/linux/list.h?

+2  A: 

Something like this:

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! */
}
Hasturkun
NOTE: this works on the proviso that you define `your_type` with a `list_head` as its first member. That ensures that the address of the `list_head` is the same as the address of the`your_type` structure.
torak
@torak: this is incorrect, it doesn't matter where the list_head member is, and you can have multiple list_head structs.The reason this works is that `list_entry` uses the `container_of` macro to get the correct offset from the struct member (here `name_of_list_head_struct_member`)
Hasturkun