Search code examples
cglibc

c programming #define struct { } declaring


I was looking at Glibc codes. Some codes of glibc's queue caught my attention. I couldn't give a meaning to this struct definition. This struct doesn't have a name. Why? How does it work?

#define LIST_ENTRY(type)                        \
struct {                                \
    struct type *le_next;   /* next element */          \
    struct type **le_prev;  /* address of previous next element */  \
}

Source


Solution

  • That is actually a preprocessor macro, that could be expanded (most probably with trailing name) somewhere else.

    In the comments at the start of that header file there is a reference to queue(3) man page that contains more details on that and other macros:

    The macro LIST_ENTRY declares a structure that connects the elements in the list.

    And an example of use:

    LIST_HEAD(listhead, entry) head = LIST_HEAD_INITIALIZER(head);  
    struct listhead *headp;                 /* List head. */
    struct entry {  
      ...  
      LIST_ENTRY(entry) entries;      /* List. */  
      ...  
    }  
    *n1, *n2, *n3, *np, *np_temp;  
    
    LIST_INIT(&head);                       /* Initialize the list. */  
    
    n1 = malloc(sizeof(struct entry));      /* Insert at the head. */   
    LIST_INSERT_HEAD(&head, n1, entries);
    

    Being this C code (not C++), and C lacks templates, this preprocessor macro can be used to "simulate" templates (note the type parameter).