I have been looking at how I could use Single linked lists using the macros defined in sys/queue.h and I have a few doubts. Currently I am trying to create a simple list using the same. Please note that the program below is incomplete, I have put in only the part I feel is relevant.
int main() {
SLIST_HEAD(slisthead, entry) head = SLIST_HEAD_INITIALIZER(head);
struct slisthead *headp;
struct entry {
SLIST_ENTRY(entry) entries;
}*n1, *n2, *n3, *np;
/* Upon expanding the macro we would get,
* struct entry {
* struct {
* struct entry *sle_first;
* } entries;
* }*n1, *n2, *np;
*/
I fail to understand why “sle_first” is stored in another struct altogether. Couldnt SLIST_ENTRY expand to something like the following instead?
#define SLIST_ENTRY(type) struct type* sle_first;
sys/queue.h also contain double linked lists and queues, that require more than one pointer in entry.
So I think SLIST_ENTRY expanded this way to be similar to another type entries, like queues.