Search code examples
cpointersabstract-data-type

dereferencing pointer to incomplete type abstract-data-type


I have the following code parts:

dictionary.h

#ifndef _DICTIONARY_H
#define _DICTIONARY_H

typedef struct _dict_t dict_t;
typedef dict_t *Dictionary;

Dictionary dict_new(void);
(...)

dictionary.c

#include "dictionary.h"
struct _dict_t {
    unsigned int size;
    char **data;
};

Dictionary dict_new(void){
    Dictionary dict = NULL;
    dict = calloc(1, sizeof(struct _dict_t));
    dict->data = calloc(1, sizeof(char));
    dict->size = 0;
    return (dict);
}

main.c

#include "dictionary.h"
Dictionary main_dict; // global dictionary
Dictionary ignored;   // Yes, i know its horrible

int is_known(char *word){
    int i;
    for (i = 0; i < main_dict->size; ++i)  {
        if (strcmp(main_dict->data[i], word) == 0)
            return 1;
    }

    for (i = 0; i < ignored->size; ++i){
        if (strcmp(ignored->data[i], word) == 0)
            return 1;
    }
    return 0;
}

int main(){
    (...)
}

One of many errors (dereferencing pointer to incomplete type) is here:

main_dict->size

I can't find the error. What is happening?


Solution

  • When the type is incomplete, the compiler does not know what is inside an instance of that type. You need to provide definition of the type before you can dereference it.