Incompatible Types When Assigning Memory in Struct

advertisements

I making a program and no know why Code::Blocks show me some error:

error: conflicting types for 'loadSavedGame'
note: previous declaration of 'loadSavedGame' was here(In headers of file)
In function 'loadSavedGame':error: incompatible types when assigning to type 'struct saveGame' from type 'struct saveGame *'

and I do not understand because show me an error, if i have anothers functions declared in the same way, look:

        struct configParam{
           char *clave;
           char *valor;
        };

        struct saveGame{
           char *id;
           char *score;
           char *position;
           char *maze_level;
           char *achievements;
           char *time_playing;
           char *virtual_players;
        };

        void loadSettings(char fileName[],struct configParam *paramsReaded[], int *length, int *statusCode);
        void saveSettings(char fileName[],struct configParam paramsToWrite[], int length, int *statusCode);

        void loadSavedGame(char fileName[],struct saveGame *savedGamesReaded[], int *length, int *statusCode);
        void saveGame(char fileName[],struct saveGame savedGamesToWrite[], int length, int *statusCode);

    void loadSettings(char fileName[],struct configParam *paramsReaded[], int *length, int *statusCode){

        *paramsReaded = (struct configParam*)malloc((*length)*sizeof(struct configParam)); //THIS WORK
        int j;
        for(j=0; j<*length; j++){
            (*paramsReaded)[j].clave = (char *)malloc(MAX_STRING*sizeof(char)); //reserva de memoria para los miembros "clave" y "valor" de la estructura
            (*paramsReaded)[j].valor = (char *)malloc(MAX_STRING*sizeof(char));
        }

    }

void loadSavedGame(char fileName[], struct saveGame *savedGamesReaded, int *length, int *statusCode){

    *savedGamesReaded = (struct saveGame*)malloc((*length)*sizeof(struct saveGame)); // HERE SHOW AN ERROR :(
    int j;
    for(j=0; j<*length; j++){
        (*savedGamesReaded)[j].id = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].score = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].position = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].maze_level = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].achievements = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].time_playing = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].virtual_players = (char *)malloc(MAX_STRING*sizeof(char));
    }

}

both functions work basically the same, only " loadsaveGame " points to a longer array, then I do not know why it does not work(function loadsaveGame) thanks for your replies guys


Lets reduce this further to an MCVE...

void loadSettings(struct configParam *paramsReaded[]){
    *paramsReaded = malloc(42); // This compiles.
}

void loadSavedGame(struct saveGame *savedGamesReaded){
    *savedGamesReaded = malloc(42); // This doesn't.
}

What's the difference? Lets investigate further by stripping the parameter identifiers and bodies to form prototypes for these two functions:

void loadSettings(struct configParam *[]);
void loadSavedGame(struct saveGame *);

Do you see the difference, now? The first one uses two levels of indirection (the * and the []), while the second one only uses one level of indirection.

The first one uses a correct idiom, if you intend to assign to an object that exists after loadSettings returns. It looks sane.

The second one attempts to use that same pattern, but it's missing the []... Hmmmm?

P.S. For future reference, please learn how to produce MCVEs so you can spot these kinds of simple typographical issues more easily by yourself.

P.P.S. Don't forget to check the return value of malloc (for NULL values) before you try to store something into the object it points to.