Search code examples
chashtableglibgcc4

What is the meaning of "array type has incomplete element type"?


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.


Solution

  • 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.