I’m working on an open-source project, and there is an hash_table
that I need to change for a more efficient hash_table, so, I’m trying
to use the header <search.h>;
The problem, is that I need to overwrite the functions that is already
being used all over the project … but for that, I need to use sizeof(struct hsearch_data)
but it doesn’t work.
Follow the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE
#include <search.h>
#include "hashtable.h"
#define MAX_ELEMENTS 100
#define ERROR 2
#define SUCCESS 0
hash_table *new_hash_table()
{
hash_table *table = (hash_table *) malloc(sizeof(struct hsearch_data));
*table = {0};
int status = hcreate_r(MAX_ELEMENTS, table);
if (status == 0) {
hdestroy_r(table);
return NULL;
}
return table;
}
PS: in the header file, there is a
typedef struct hsearch_data hash_table;
I got the error message:
hashtable.c: In function ‘new_hash_table’:
hashtable.c:18: error: invalid application of ‘sizeof’ to incomplete type ‘struct hsearch_data’
hashtable.c:19: error: dereferencing pointer to incomplete type
Can anybody help me out?
Apparently, the trouble is that the
<search.h>header on your platform does not define the structure type. As noted in a comment to the question, on a RHEL5 Linux machine, the defines ‘struct hsearch_data‘ when__USE_GNUis defined, which is, in turn, defined when_GNU_SOURCEis defined as you have it. However, not all machines are Linux. I note that POSIX does define the<search.h>header, but does not specify the structure you are seeking to use.You will need to track down where the project defines the structure, and decide how you can make that available to this code. It may be simple – if the structure is safely segregated in a header which can be safely included. It may be complex if the header that defines the structure also defines other things which you can’t use.
Since you are trying to preserve the same interface as the original code, you should be aiming to use the original header to provide the correct interface definition.