I generally have ignored using macros while writing in C but I think I know fundamentals about them. While I was reading the source code of list in linux kernel, I saw something like that:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
(You can access the remaining part of the code from here.)
I didn’t understand the function of ampersands(I don’t think they are the address of operands here) in LIST_HEAD_INIT and so the use of LIST_HEAD_INIT in the code. I’d appreciate if someone can enlighten me.
To know what actually is happening we need the definition of
struct list_head:Now consider the macros:
If in the code I write
LIST_HEAD(foo)it gets expanded to:which represents an empty doubly linked list with a header node where the
nextandprevpointers point to the header node itself.It is same as doing:
So effectively these macros provide a way to initialize a doubly linked list.
And yes,
&is used here as the address of operator.EDIT:
Here is a
working exampleIn the link provided by you. You had:
which is incorrect. You should have: