#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <assert.h>

char *data[] = { "alpha", "bravo", "charlie", "delta",
      "echo", "foxtrot", "golf", "hotel", "india", "juliet",
      "kilo", "lima", "mike", "november", "oscar", "papa",
      "quebec", "romeo", "sierra", "tango", "uniform",
      "victor", "whisky", "x-ray", "yankee", "zulu"
       };

int
main(void)
{
    ENTRY e, **ep;
    struct hsearch_data *htab;
    int i;
    int resultOfHcreate_r;
    resultOfHcreate_r=hcreate_r(30,htab);
    assert(resultOfHcreate_r!=0);
    hdestroy_r(htab);
    exit(EXIT_SUCCESS);
}
hcreate_r 中的错误

如何使用这个 hcreate_r

另一个问题是:

您能否提供 GNU 扩展 C 库示例?
我认为GNU扩展C库的文档知识不够写。

我有很多关于如何使用扩展 C 库的问题。

最佳答案

首先,您需要添加 #define _GNU_SOURCE 宏才能正确访问 GNU 扩展。 IE:

#define _GNU_SOURCE
#include <search.h>

然后你需要了解文档:



因此,与 hcreate 不同,您提供的是散列表数据结构。此外,这些结构应该初始化为零。那么你可能想要做这样的事情:
//dynamically create a single table of 30 elements
htab=calloc(1,sizeof(struct hsearch_data));
resultOfHcreate_r=hcreate_r(30,htab);

//do some stuff

//dispose of the hash table and free heap memory
hdestroy_r(htab);
free(htab)

关于c - 如何使用 GNU hcreate_r,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10551366/

10-13 05:27