Search code examples
cabstract-data-type

problems with typedef on C


I writing some ADT on C: I have two file date.c and date.h inside date.c I have:

typedef struct Date_t {
 int date;
 char* month;
 int year;

} Date;

inside date.h I have:

typedef Date pDate;

compiler gives me errors:

..\checking.h:15: error: syntax error before "pDate"

can somebody please explain what is wrong with my typedef, thanks in advance

EDIT:

with files all is ok, problem is, when I change my struct to:

struct Date_t {
     int date;
     char* month;
     int year;
    
    };

and pointer to:

typedef struct Date_t* pDate;

program works perfectly, so I want to understand the difference


Solution

  • You are probably declaring pDate as a typedef of Date before you even declared Date (since you are make pDate in the header).

    You could do:

    typedef struct Date_t {
     int date;
     char* month;
     int year;
    
    } Date;
    
    typedef Date pDate;
    

    All in you .c file.

    As for your edit:

    You are declaring the struct in the typedef:

    typedef struct Date_t* pDate;
    

    It is working because you are declaring struct before you place pDate.

    typedef /*see this struct here*/ struct /*huh?*/ Date_t* pDate;
    

    The other has no struct in it.