What is the meaning of “array type has incomplete element type”?
Domanda
I have the following simple lines of code:
#include <glib.h>
#include <stdio.h>
void my_func () {
GHashTable htbls[3]; /* ASSUME LINE NUMBER IS N */
/* Do something */
}
int main (int argc, char *argv[]) {
my_func ();
return 0;
}
But
$gcc `pkg-config --cflags --libs glib-2.0` ./main.c
gives the following error:
./main.c:N: error: array type has incomplete element type
I don't understand why the element type is incomplete. GHashTable
is completely specified in glib.h
.
Thanks in advance for your help.
Soluzione
It presumably means that GHashTable
is not completely defined in the headers you include. That is, there is likely a line in glib.h
or one of the files it includes that reads something like:
typedef struct GHashTable GHashTable;
The structure tag could be different without changing my argument. There must be something similar because otherwise you'd get a different message about GHashTable
not being recognized as a type name:
error: unknown type name 'GHashTable'
There is no extra information provided about the structure in <glib.h>
; you don't need to know it to use it. The API for the hash table functions probably deal with GHashTable *
values only, so you don't need to know what's inside, any more than you need to know what's inside a FILE *
to be able to use it in your code (though macroized functions such as getchar()
might need to know about the internals of FILE *
; maybe a better analogy is DIR *
, but that's a POSIX interface and not necessarily as well known).
It means you will need to use:
GHashTable *htbls[3];
You can have arrays of pointers to incomplete types without problem.
Altri suggerimenti
Are you sure about that? Copy/paste the specification from glib.h. (My glib.h does not contain a definition for GHashTable).
You need to use a pointer, not a value:
GHashTable* htbls[3];
Q: Isn't GHashTable is completely specified in glib.h?
A: No.
If you want a hashtable, you should use g_hash_table_new(), g_hash_table_add () and friends:
In other words, "struct _GHashTable" is opaque to your application.
If you really want an array of GHashTables, then you'll need an array of "GHashTable *". And you'll need to explicitly initialize each element of your array with its own, new hash table.