分段错误 - 无法找到原因(使用GDB调试)

时间:2016-11-29 18:47:13

标签: c segmentation-fault

我得到了一个我不理解的段错误。我有以下代码:

在主要分配填充结构团队和调用函数时: TEAM团队[NUMBER_OF_TEAMS];

  /* Initializing af team array*/
  int i = 0;
  int k = 0;

  for(i=0; i<NUMBER_OF_TEAMS; i++) {
    team[i].name = round[0].match[k].home_team;
    i++;
    team[i].name = round[0].match[k].away_team;
    k++;
  }  // The team array is filled rather funny because of the structure of the data I am using to fill it. There are 12 teams in a round of soccer matches and 6 home-teams and 6 away teams. Hence round[0] for both and this method.

int q = 0;
for(q = 0; q<NUMBER_OF_TEAMS; q++) {
  team[q].spectators_home_last_year = 0;
  team[q].matches_won = 0;
  team[q].matches_won_away = 0;
  team[q].matches_won_home = 0;
}




solve_task_three(round, team, teams_winning_away, &array_length); // Debugger claims

功能:

void solve_task_three(ROUND *round, TEAM *team, TEAM *teams_winning_away, int *array_length) { /* Wins more away than home*/
  int i = 0;
  int j = 0;
  int k = 0;


  for(i=0; i<ROUNDS_PR_SEASON; i++) { 
    for(j=0; j<MATCH_PR_ROUND; j++) {
      int home_team_wins = round[i].match[j].home_team_score > round[i].match[j].away_team_score;
      int out_team_wins = round[i].match[j].home_team_score < round[i].match[j].away_team_score;

      if(home_team_wins) {
        // GDB says this line is causing the segfault
        while(strcmp(round[i].match[j].home_team,team[k].name) != 0) { 
          k++;
        }
        team[k].matches_won_home++;
        team[k].matches_won++;
      }

      else if (out_team_wins) {
          while(strcmp(round[i].match[j].away_team, team[k].name) != 0) { // 
            k++;
          }
          team[k].matches_won_away++;
          team[k].matches_won++;
        }
      }
    }
  }

我根本不明白为什么会在这个位置引起段错误。有什么想法吗?

编辑:在while循环中插入打印语句,输出:

i=0 j=0 round[i].match[j].home_team: FCN team[k].name:SDR
i=0 j=1 round[i].match[j].home_team: FCM team[k].name:FCM
i=0 j=2 round[i].match[j].home_team: OB team[k].name:VFF
i=0 j=2 round[i].match[j].home_team: OB team[k].name:OB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:HOB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:AGF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:BIF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:AAB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:EFB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:FCK
i=1 j=0 round[i].match[j].home_team: HOB team[k].name:RFC
Segmentation fault: 11

使用i和j值进行迭代的开始和结束。

i=0 j=0 round[i].match[j].home_team: FCN team[k].name:FCN
i=0 j=0 round[i].match[j].home_team: FCN team[k].name:SDR
i=0 j=1 round[i].match[j].home_team: FCM team[k].name:SDR
i=0 j=1 round[i].match[j].home_team: FCM team[k].name:FCM
i=0 j=2 round[i].match[j].home_team: OB team[k].name:FCM
i=0 j=2 round[i].match[j].home_team: OB team[k].name:VFF
i=0 j=2 round[i].match[j].home_team: OB team[k].name:VFF
i=0 j=2 round[i].match[j].home_team: OB team[k].name:OB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:OB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:HOB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:HOB
i=0 j=3 round[i].match[j].home_team: AGF team[k].name:AGF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:AGF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:BIF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:BIF
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:AAB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:AAB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:EFB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:EFB
i=0 j=5 round[i].match[j].home_team: FCK team[k].name:FCK
i=1 j=0 round[i].match[j].home_team: HOB team[k].name:FCK
i=1 j=0 round[i].match[j].home_team: HOB team[k].name:RFC
i=1 j=0 round[i].match[j].home_team: HOB team[k].name:RFC
Segmentation fault: 11


typedef struct {
  char name[TEAM_SIZE];
  int points, matches_played, 
    matches_won, matches_draw, matches_lost, 
    matches_won_home, matches_won_away,
    goals_for, goals_against, goal_difference;
    double spectators_home_last_year;
} TEAM;

0 个答案:

没有答案