Search code examples
cfunctionstructtypedefargument-passing

Function does not recognize typedef argument


okay, ive searched a solution for like two days now but i couldnt find whats going wrong with my code. ;(

The task is simple: define a new type using typedef and have a function read out lines of this new type from a file into an array of again this new type. so my typedef inside the headerfile looks like this right now (ive tried several variants of writing this)

// filename: entries.h
#ifndef ENTRIES_H_
#define ENTRIES_H_
#include<time.h>

typedef struct{
    char Loginname[25];
    time_t RegDate;
    unsigned long Highscore;
    time_t Hdate;
}typePlayerEntry;

int readPlayerList(char *name, typePlayerEntry *feld);

#endif /* ENTRIES_H_ */

the main.c:

//filename: main.c

#include <stdio.h>
#include "entries.h"


int main(void) {
    char name[13]="numbers.txt";
    typePlayerEntry *pep;

    readPlayerList(name, pep);

    return 0;
}

my function file looks like this (and heres where the error is shown)

//filename: readPlayerList.c

int readPlayerList(char *name, typePlayerEntry *feld) {
return 0;
}

irrelevant code is completely left out. The problem is reproducable with the code posted.

the program wont compile because the type of the second argument in the function file could not be recognized, - which is odd, because its defined in the header file and also usable in the main function. And this error is somehow connected to the declaration of (in this case) a pointer of type playerEntry in my main.c. So if i do not declare it, theres no error, though i have to declare it to actually give it to the function. how come that the solution so far is to include the entries.h into the readPlayerList.c, which wasnt neccesary for previous functions?

im using eclipse kepler with MinGW, in case thats a compiler issue.

corrected the missing include of time.h and adjusted the code a little.


Solution

  • You are missing #include <time.h> in entries.h.

    // filename: entries.h
    #ifndef ENTRIES_H_
    #define ENTRIES_H_
    
    typedef struct {
        char Loginname[25];
        time_t RegDate;                  /* from <time.h> */
        unsigned long Highscore;
        time_t Hdate;                    /* from <time.h> */
    } playerEntry;
    
    int readPlayerList(char *name, playerEntry *feld);
    
    #endif /* ENTRIES_H_ */
    

    And you need to #include "entries.h" in readPlayerList.c

    //filename: readPlayerList.c
    
    int readPlayerList(char *name, typePlayerEntry *feld) {
    /*                             ^^^^^^^^^^^^^^^ from entries.h */
    return 0;
    }