Using macros in C to define data structures

后端 未结 1 1278
南笙
南笙 2021-01-19 03:47

I am trying to wrap my head around the concept of using macros to define data structure operations. The following code is a simple example to use the built in list library i

相关标签:
1条回答
  • 2021-01-19 03:50

    First read this to get a hold what these macros do. And then go to queue.h. You'll get your treasure trove there!

    I found a few gold coins for you-

    #define STAILQ_HEAD(name, type)                                         \
    struct name {                                                           \
            struct type *stqh_first;/* first element */                     \
            struct type **stqh_last;/* addr of last next element */         \
    }
    

    Lets dig in a bit deep and answer your questions

    What is stailhead? This seems to be "just" defined.

    #define STAILQ_HEAD(name, type)                                         \
    struct name {                                                           \
            struct type *stqh_first;/* first element */                     \
            struct type **stqh_last;/* addr of last next element */         \
    }
     STAILQ_HEAD(stailhead, entry) head =
     STAILQ_HEAD_INITIALIZER(head);
     struct stailhead *headp;            /* Singly-linked tail queue head. */
    

    So stailhead is a structure

    How to pass head and entries to a function?

    #define STAILQ_ENTRY(type)                                              \
    struct {                                                                \
            struct type *stqe_next; /* next element */                      \
    }
    

    So entries and head ( as explained before ) are just structures and you can pass them just as you pass other structures. &structure_variable

    What type is head, how can I declare a pointer to it?

    Already explained!

    Read this man page for nice pretty examples.

    0 讨论(0)
提交回复
热议问题