简体   繁体   中英

Getting a segmentation error in my C program while running tests

/* Created Library File */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include "tournament.h"

Team * initializeTeam(char * name, Player * players){
    Team *team = malloc(sizeof(Team));
    (*team).name = name;
    (*team).players = players;
    return team;
}
Team * game(Team * team1, Team * team2){
    int turns = 10;
    int team_A_total = 0;
    int team_B_total = 0;
    int team_A_shooting = 0;
    int team_B_shooting = 0;
    int team_A_blocking = 0;
    int team_B_blocking = 0;
    int team_A_def = 0;
    int team_B_def = 0;
    int team_B_off = 0;
    int team_A_off = 0;
   for(int i=0;i<turns;i++){
     team_A_off = team_A_off + (*team1).players[i].offensive;
     team_A_def = team_A_def + (*team1).players[i].defensive;
     team_B_off = team_B_off + (*team2).players[i].offensive;
     team_B_def = team_B_def + (*team2).players[i].defensive;
   };
   for(int j=0;j<turns;j++){
     team_A_shooting = rand() % team_A_off; 
     team_A_blocking = rand() % team_A_def;
     team_B_shooting = rand() % team_B_off;
     team_B_blocking = rand() % team_B_def;

   if(team_A_shooting>team_B_blocking){
     team_A_total = (team_A_total + 1);
   };
   if(team_B_shooting>team_A_blocking){
     team_B_total = (team_B_total + 1);
   };
   };
   if(team_A_total >  team_B_total){
     return team1; 
   }
   else
    if(team_B_total > team_A_total){
            return team2;
   }
   else 
    if(team_A_total == team_B_total){
            int number = rand();
            if(number % 2 == 0){
                return team1;}
            else{
                return team2;}
   }
 return 0;
}
/*header file*/
#ifndef JG_TOURNEY_H
#define JG_TOURNEY_H
typedef struct Player{
   int offensive;
   int defensive;
   int number;
   int team;
   char first[255];
   char last[255];
} Player;

typedef struct Team{
   char * name;
   Player * players;
} Team;

Team * initializeTeam(char * name,Player * players);
Team * game(Team * team1, Team * team2);
Team * tournament(Team **, int);
void deleteTeam(Team *);

#endif
/*Driver code*/
#include"tournament.h"
#include"tournament.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>

#define DEBUG 0

#define NUM_TEAMS 32
#define NUM_PLAYERS 10

Player * draftPlayers(char * filename, int team, int num_players);
int inLeague(Team *, Team **);

int main(){
    /*****  Change the team names to names of your choosing ******/
    char * team_names[] = {
        "team1",
        "team2",
        "team3",
        "team4",
        "team5",
        "team6",
        "team7",
        "team8",
        "team9",
        "team10",
        "team11",
        "team12",
        "team13",
        "team14",
        "team15",
        "team16",
        "team17",
        "team18",
        "team19",
        "team20",
        "team21",
        "team22",
        "team23",
        "team24",
        "team25",
        "team25",
        "team27",
        "team28",
        "team29",
        "team30",
        "team31",
        "team32",
    };

    /********************************************************************/
    /*    DO NOT ALTER ANY OF THE THE DRIVER CODE BELOW                 */
    /********************************************************************/
#if !DEBUG
    fclose(stderr);
#endif
    printf("\t-----------------------------------\n");
    printf("\t-      Creating Your League       -\n");
    printf("\t-----------------------------------\n");
    srand(time(NULL));

    printf("\n\t=========Test #1: Creating your Teams and Players===========\n\n");
    Team ** league = malloc(sizeof(Team*) * NUM_TEAMS);
    //create teams i.e. league of N teams.
    for(int counter = 0; counter < NUM_TEAMS; counter++){
        Player * draft = draftPlayers("players.dat", counter, NUM_PLAYERS);
        assert(draft[0].team == counter%NUM_TEAMS);
        league[counter] = initializeTeam(team_names[counter], draft);
        assert(sizeof((league[counter]->players[rand() % NUM_PLAYERS])) == sizeof(Player));
        assert((league[counter]->players[rand() % NUM_PLAYERS]).offensive > 0);
        assert((league[counter]->players[rand() % NUM_PLAYERS]).defensive > 0);
        assert(sizeof(*(league[counter])) == sizeof(Team));
    }
    printf("\n\t\t....League Initialized\n");

    printf("\t-----------------------------------\n");
    printf("\t-   Part 1: Exhibition Games      -\n");
    printf("\t-----------------------------------\n");

    printf("\n\t=========Test #1: Sanity check for the game===========\n\n");

    Team * winner = game(NULL, NULL);
    assert(winner == NULL);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Test #2: Playing a Game between two random teams===========\n\n");
    Team * team1 = league[rand() % NUM_TEAMS];
    Team * team2 = league[rand() % NUM_TEAMS];
    printf("Up next, an exhibition game between %s and %s\n", team1->name, team2->name);
    winner = game(team1, team2);
    assert(winner == team1 || winner == team2);
    printf("The winning team is %s\n\n", winner->name);
    printf("\n\t\t....Test Passed\n");

    printf("\n\t=========Test #3: Playing a Game between the same team===========\n\n");
    int team_num = rand() % NUM_TEAMS;
    printf("Up next, a a scrimmage for %s\n", league[team_num]->name);
    winner = game(league[team_num], league[team_num]);
    assert(winner == league[team_num]);
    printf("The winning team is %s\n\n", winner->name);
    printf("\n\t\t....Test Passed\n");

When running the created library file and running it with the driver code I fail the first test because of a segmentation fault. I know it has to do with something with my memory however I am unable to figure out where my code is causing this. I know it is a lot if code to deal with however I only included the parts of the code that is needed to run this test. Any help is very much appreiated.

(*team).name = name;
(*team).players = players;

It looks like you're trying to dereference team using * , then dereference it again using .name , so try removing the * and dereference with .name or ->name only.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM