My code won't build and returns the error of dereferencing when I try to do anything with TournamentKey, like:
TournamentKey new_tournament_key=(TournamentKey)malloc(sizeof(new_tournament_key));
new_tournament_key->tournamentId=tournament_id;
Why is this happening? I think I have added everything, including in the cMake list..
in tournaments.h
#ifndef CHESS_TOURNAMENTS_H
#define CHESS_TOURNAMENTS_H
#include <stdbool.h>
typedef struct tournament_t *Tournament;
typedef struct tournament_key_t *TournamentKey;
MapDataElement copyTournamentData(MapDataElement dataElement);
MapKeyElement copyTournamentKey(MapKeyElement key);
void freeTournamentData(MapDataElement value);
void freeTournamentKey(MapKeyElement key);
int compareTournamentKeys(MapKeyElement key1, MapKeyElement key2);
#endif /* CHESS_TOURNAMENTS_H */
in tournaments.c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "tournaments.h"
#define KEYS_EQUAL 0
#define FIRST_KEY_BIGGER 1
#define SECOND_KEY_BIGGER -1
#define INVALID_KEY 2
struct tournament_t{
int tournamentId;
char* tournamentsPlace;
int numberOfGames;
int tournamentWinner;
int maxGamesPerPlayer;
Map games;
};
struct tournament_key_t{
int tournamentId;
};
MapDataElement copyTournamentData(MapDataElement dataElement)
{
Tournament new_tournament = (Tournament) malloc(sizeof *new_tournament);
if(!new_tournament){
return NULL;
}
new_tournament->tournamentWinner = ((Tournament) dataElement)->tournamentWinner;
new_tournament->numberOfGames = ((Tournament) dataElement)->numberOfGames;
new_tournament->tournamentId = ((Tournament) dataElement)->tournamentId;
new_tournament->maxGamesPerPlayer = ((Tournament) dataElement)->maxGamesPerPlayer;
new_tournament->tournamentsPlace = malloc(strlen(((Tournament) dataElement)->tournamentsPlace)+1);
if (new_tournament->tournamentsPlace) {
memset(new_tournament->tournamentsPlace, '\0', sizeof *new_tournament->tournamentsPlace);
strcpy(new_tournament->tournamentsPlace, ((Tournament) dataElement)->tournamentsPlace);
}
new_tournament->games = mapCopy(((Tournament) dataElement)->games);
return new_tournament;
}
MapKeyElement copyTournamentKey(MapKeyElement key)
{
// TournamentKey temp_key = (TournamentKey)key;
TournamentKey new_key = (TournamentKey)malloc(sizeof *new_key);
if (!new_key){
return NULL;
}
new_key->tournamentId = ((TournamentKey) key)->tournamentId;
return (MapKeyElement)new_key;
}
void freeTournamentData(MapDataElement value)
{
// value is a data element in Tournaments Map i.e. its a tournament.
// First mapDestroy the map of games in the current tournamnet,
// Then free the tournament intself.
Tournament curr_tour = (Tournament)value;
// free(curr_tour->tournamentsPlace);
mapDestroy(curr_tour->games);
free(curr_tour);
}
void freeTournamentKey(MapKeyElement key)
{
free(key);
}
int compareTournamentKeys(MapKeyElement key1, MapKeyElement key2)
{
if(!key1 || !key2)
{
printf("compareTournamentKeys: You have got a NULL key\n");
return INVALID_KEY;
}
TournamentKey new_key1 = (TournamentKey)key1;
TournamentKey new_key2 = (TournamentKey)key2;
if(new_key1->tournamentId > new_key2->tournamentId)
{
return FIRST_KEY_BIGGER;
}
else if(new_key1->tournamentId == new_key2->tournamentId)
{
return KEYS_EQUAL;
}
else
{
return SECOND_KEY_BIGGER;
}
}
and in chess.c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "chess.h"
#include "games.h"
#include "tournaments.h"
#define KEYS_EQUAL 0
#define FIRST_KEY_BIGGER 1
#define SECOND_KEY_BIGGER -1
#define INVALID_KEY 2
struct chess_t{
Map mapOfTournaments;
};
ChessSystem chessCreate()
{
ChessSystem chess_game_system=(ChessSystem)malloc(sizeof(*chess_game_system));
if(!chess_game_system)
return NULL;
chess_game_system->mapOfTournaments=mapCreate(©TournamentData,
©TournamentKey,
&freeTournamentData,
&freeTournamentKey,
&compareTournamentKeys);
return chess_game_system;
}
int playerGamesPlayed(ChessSystem chess, int tournament_id, int player_id)
{
int num_of_games_played=0;
TournamentKey new_tournament_key=(TournamentKey)malloc(sizeof(new_tournament_key));
new_tournament_key->tournamentId=tournament_id;
Tournament curr_tournament=mapGet(chess->mapOfTournaments, new_tournament_key);
MAP_FOREACH(GameKey, curr_game_key, curr_tournament->games){
if (curr_game_key){
int curr_player_first_id=curr_game_key->firstPlayerId;
int curr_player_second_id=curr_game_key->secondPlayerId;
if(curr_player_first_id == player_id || curr_player_second_id == player_id){
num_of_games_played++;
}
freeGameKey(curr_game_key);
}
}
freeTournamentKey(new_tournament_key);
return num_of_games_played;
}
Why is this happening?
Because struct tournament_key_t {...}
definition is inside tournaments.c
, so it's not visible inside chess.c
. If you want it to be visible, you could copy it, or you could move it from tournaments.c
to tournaments.h
, as chess.c
does #include "tournaments.h"
.
typedef struct tournament_key_t *TournamentKey;
Do not use typedef pointers - they are confusing. Prefer to do typedef struct tournament_key_t TournamentKey;
and just write the *
- your code will be way much clearer and easier.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.