Search code examples
ctypedef

typedef redefinition error while trying to compile


dictionary.h :

#ifndef __DICTIONARY_H
#define __DICTIONARY_H

typedef struct Dictionary Dictionary;
.
.
.
#endif

dictionary.c :

typedef struct Dictionary{

    int* keys;
    int* values;
    int topIndex;
    int keysSize;
    int valuesSize;

}Dictionary;

I was told by my instructor to not implement the typedef in the .h file so I did it in my .c file but I'm getting errors about redefining it when I try to compile.

I tried to delete the definition in the .h file but got errors because my function who return the typedef now return a non defined type.

How can I solve it?


Solution

  • C before C11 doesn't support typedef redefinitions.

    If you've included a header containing typedef struct Dictionary Dictionary;, your struct Dictionary definition shouldn't reintroduce the typedef name if you want to support those older C implementations. I.e., just do struct Dictionary{ /*...*/ }; in the C file.