简体   繁体   中英

Dereferencing pointer to incomplete type [Working on structs]

I have declared:

#include stdio.h

#include stdlib.h

#include string.h

#include dictionary.h

int main( int argc, char ** argv ){

    char * dictionary_name = DEFAULT_DICTIONARY;
    dictionary_t dictionary;
    dictionary->entries = 1;
    if ( dictionary == NULL){
        printf("NULL\n");
        return -1;
    }
    return 0;
}

Error:

src/main.c: In function ‘main’:
src/main.c:40:12: error: dereferencing pointer to incomplete type ‘struct dictionary_s’
dictionary->entries = 1;

In dictionary.c:

#include dictionary.h

struct dictionary_s{

    char * name;
    llist_t content;
    int entries;
};

In header (dictionary.h):

typedef struct dictionary_s* dictionary_t;

It's my first time asking a question in here, so, please forgive me if I'm missing something important.

The file dictionary.h only contains the name of the type struct dictionary_s , so that is all that is visible to your main function. This means it doesn't know what the structure contains.

You need to move the definition of struct dictionary_s into the header file. That way it can be used from main .

You have to place the definition of the struct in the header file, not in the dictionary.c -file; otherwise, its components are not known outside of dictionary.c .

So your dictionary.h should look as follows:

struct dictionary_s{

    char * name;
    llist_t content;
    int entries;
};

typedef struct dictionary_s* dictionary_t;

Your file dictionary.c is probably obsolete then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM