在C使用宏来定义数据结构 [英] Using macros in C to define data structures

查看:726
本文介绍了在C使用宏来定义数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图环绕使用宏来定义数据结构,经营的理念我的头。下面code是一个简单的例子来使用内置列表库在FreeBSD中。库中的所有操作都定义为宏。我见过几个其他图书馆也这种做法。

我可以看到这个有一定的优势如。是可以使用任何数据结构,列表中的一个元素的能力。但我不明白这是如何工作。例如:


  1. 什么是 stailhead ?这似乎是公正的定义。

  2. 如何通过来的函数吗?

  3. 是什么类型的,我怎么可以声明指向它的指针?

有没有这个技术,我可以用它来搜索谷歌,或解释了这个概念,任何一本书一个标准名称?这种技术是如何工作的任何链接或很好的解释,将大大AP preciated。

感谢尼克拉斯B. 我跑了的gcc -E 和得到这个定义

 结构stailhead {
  结构stailq_entry * stqh_first;
  结构stailq_entry ** stqh_last;
}头= {((无效*)0),及(头).stqh_first};

和本作 stailq_entry

 结构stailq_entry {
 int值;
 结构{结构stailq_entry * stqe_next; }条目;
};

所以我猜的类型为结构stailhead

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / queue.h>结构stailq_entry {
        int值;
        STAILQ_ENTRY(stailq_entry)条目;
};INT主要(无效)
{
        STAILQ_HEAD(stailhead,stailq_entry)头= STAILQ_HEAD_INITIALIZER(头);
        结构stailq_entry * N1;
        无符号我;
        STAILQ_INIT(安培;头); / *初始化队列。 * /        对于(I = 0; I&小于10;我++){
                N1 =的malloc(sizeof的(结构stailq_entry)); / *将打头阵。 * /
                N 1>值=我;
                STAILQ_INSERT_HEAD(安培;头,N1,项);
        }
        N1 = NULL;        (!STAILQ_EMPTY(安培;头)){时
                N1 = STAILQ_LAST(安培;头,stailq_entry,项);
                STAILQ_REMOVE(安培;头,N1,stailq_entry,项);
                的printf(N2数:%d \\ n,N 1>值);
                免费(N1);
        }        返回(0);
}


解决方案

首先阅读这个获得什么保持这些宏做的。然后转到 queue.h 。你会得到你的宝库那里!

我发现了一些金币为你 -

 的#define STAILQ_HEAD(名称,类型)\\
结构名{\\
        结构类型* stqh_first; / *第一个元素* / \\
        结构类型** stqh_last; / *最后一个元素的地址* / \\
}

让我们挖了一点深,回答您的问题


  

什么是stailhead?这似乎是公正的定义。


 的#define STAILQ_HEAD(名称,类型)\\
结构名{\\
        结构类型* stqh_first; / *第一个元素* / \\
        结构类型** stqh_last; / *最后一个元素的地址* / \\
}
 STAILQ_HEAD(stailhead,输入)头=
 STAILQ_HEAD_INITIALIZER(头);
 结构stailhead *耳机套装; / *单链表尾队列头。 * /

所以 stailhead 是一个结构


  

如何头部和条目传递给函数?


 的#define STAILQ_ENTRY(类型)\\
结构{\\
        结构类型* stqe_next; / *下一个元素* / \\
}

所以(如前所述)都只是结构,你可以通过它们,就像你传递其他结构。 &放大器; structure_variable


  

是什么类型的头,我怎么可以声明指向它的指针?


已经解释!

这个男人页面了不错的pretty例子。

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 in FreeBSD. In the library all operations are defined as macros. I have seen this approach in couple of other libraries also.

I can see that this has some advantages eg. being ability to use any data structure as an element in the list. But I do not quite understand how this works. For example:

  1. What is stailhead? This seems to be "just" defined.
  2. How to pass head and entries to a function?
  3. What type is head, how can I declare a pointer to it?

Is there a standard name for this technique which I can use to search google, or any book which explains this concept? Any links or good explanation as to how this technique works will be much appreciated.

Thanks to Niklas B. I ran gcc -E and got this definition for head

struct stailhead {
  struct stailq_entry *stqh_first;
  struct stailq_entry **stqh_last; 
} head = { ((void *)0), &(head).stqh_first };

and this for stailq_entry

struct stailq_entry {
 int value;
 struct { struct stailq_entry *stqe_next; } entries;
};

So I guess head is of type struct stailhead.

#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>

struct stailq_entry {
        int value;
        STAILQ_ENTRY(stailq_entry) entries;
};

int main(void)
{
        STAILQ_HEAD(stailhead, stailq_entry) head = STAILQ_HEAD_INITIALIZER(head);
        struct stailq_entry *n1;
        unsigned i;
        STAILQ_INIT(&head);                     /* Initialize the queue. */

        for (i=0;i<10;i++){
                n1 = malloc(sizeof(struct stailq_entry));   /* Insert at the head. */
                n1->value = i;
                STAILQ_INSERT_HEAD(&head, n1, entries);
        }
        n1 = NULL;

        while (!STAILQ_EMPTY(&head)) {
                n1 = STAILQ_LAST(&head, stailq_entry, entries);
                STAILQ_REMOVE(&head, n1, stailq_entry, entries);
                printf ("n2: %d\n", n1->value);
                free(n1);
        }

        return (0);
}

解决方案

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.

这篇关于在C使用宏来定义数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆