在我的代码中出现分段错误。

时间:2014-02-03 19:09:45

标签: c arrays pointers

当尝试在Linux上运行我的程序时,我遇到了分段错误。我是C的新手。在进行研究时,我认为原因可能是一个循环超出了其中一个数组中的最后一个索引并且不应该访问内存。任何提示?


节目概述:以特定格式接收文件,并根据选民的三个最受欢迎和最不喜欢的内容,从该文件中确定最佳吃饭地点。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int outputToFile (int winner, char **restaurantList, char **voterNameList, int numberOfVoters, int **voterFavorites) {
int index;
FILE *output;

output = fopen ("output_1", "w"); //write mode

fprintf(output, "%s\n", restaurantList[winner]); // write out the winning restuarant
fprintf(output, "Happy:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the happy persons
    if (voterFavorites[index][1] == winner || voterFavorites[index][2] == winner || voterFavorites[index][3] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }   
}

fprintf (output, "Sad:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the sad persons
    if (voterFavorites[index][4] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }
}

fclose (output);
}

int countScore (int **voterFavorites, int tallyCard[], int numberOfVoters, int numberOfRestaurants) {
int index;

for (index = 0; index < numberOfRestaurants; index++ ) {
    tallyCard[index] = 0;
}

for (index = 0; index < numberOfVoters; index++) {
    if (voterFavorites[index][1] != -999) {
        tallyCard[voterFavorites[index][1]] = tallyCard[voterFavorites[index][1]] + 1;
    } else if (voterFavorites[index][2] != -999) {
        tallyCard[voterFavorites[index][2]] = tallyCard[voterFavorites[index][2]] + 1;
    } else if (voterFavorites[index][3] != -999) {
        tallyCard[voterFavorites[index][3]] = tallyCard[voterFavorites[index][3]] + 1;
    }

    if (voterFavorites[index][4] != -999) {
        tallyCard[voterFavorites[index][4]] = tallyCard[voterFavorites[index][4]] - 1;
    }
}
}

int determineWinner( int tallyCard[], int maxVotes, int **voterFavorites) {
int x, y, max = 0, min = 999, maxIndex, percent; 

for ( x = 0 ; x < 20 ; x++ ) { 
    if (tallyCard[x] > max) {
        maxIndex = x;
        max = tallyCard[x];
    } 
    if (tallyCard[x] < min) {
        if (tallyCard[x] != -999) {
            min = tallyCard[x];
        }
    }
}

percent = max/maxVotes * 100;
if ( percent >= 50) {
    return maxIndex;
} else {
    for(x = 0; x < maxVotes ; x++) {
        for (y = 0; y < 4; y++) {
            if (voterFavorites[x][y] == min) {
                voterFavorites[x][y] == -999;
            }
        } 
    }
    return -444;
}
}

void inputFromFile (void) {
int index, numberOfRestaurants, numberOfVoters, winner;
char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;
char **voterNameList = malloc(100 * sizeof(char*));
int **voterFavorites = (int**) calloc(100, sizeof(int*)), tallyCard[20];
int *fav1, *fav2, *fav3, *notFav;
FILE *input;

for ( index = 0; index < 100; index++ ) {
    voterFavorites[index] = (int*) calloc(4, sizeof(int));
    voterNameList[index] = malloc(26 * sizeof(char));
}
for ( index = 0 ; index < 20; index++ ) {
    restaurantList[index] = malloc(51 * sizeof(char));
}

input = fopen ("input_1", "r"); // !!! need to check if this will give an error. Refer to slide set 2-48
if( input == NULL ) { 
    perror("Error while opening the file.\n"); 
    exit(1);
} 

//get names of restaurants
fgets(nor, 51, input);
numberOfRestaurants = atoi(nor); //resolve char * to int
for ( index = 0 ; index < numberOfRestaurants ; index++ ) {
    fscanf(input, "%s", restaurantList[index]);
}

//get info lines and names of voters and initial values
fgets(nov, 101, input);
numberOfVoters = atoi(nov); //resolve char * to int
for ( index = 0 ; index < numberOfVoters ; index++ ) {

    fscanf(input, "%s %i %i %i %i", voterNameList[index], fav1, fav2, fav3, notFav);
    voterFavorites[index][1] = *fav1-1;
    voterFavorites[index][2] = *fav2-1;
    voterFavorites[index][3] = *fav3-1;
    voterFavorites[index][4] = *notFav-1;
}

//count total score for resturants
countScore (voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
winner = determineWinner(tallyCard, numberOfVoters, voterFavorites); // !!! probably need to look into these max votes
while (winner < 0) {
    countScore(voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
    winner = determineWinner(tallyCard, numberOfVoters, voterFavorites);
}

outputToFile(winner, restaurantList, voterNameList, numberOfVoters, voterFavorites);
}

int main (void) 
{
 inputFromFile();
return 0;   
}

我已经在这方面工作了一段时间,但我确信这可以清理一下。对不起!

1 个答案:

答案 0 :(得分:1)

char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;

/* ... */

fgets(nor, 51, input);

nor对象未初始化。