Błąd segmentacji (zrzucony rdzeń) odczytany ze standardowego wejścia

Próbuję policzyć liczbę każdego słowa w pliku. Plik może być stdin lub nazwą pliku podaną w linii poleceń (./ count -f). Do tej pory program daje poprawne wyjścia podczas odczytu pliku z linii poleceń. Ale pojawia się błąd, gdy próbuję odczytać ze standardowego wejścia. Program najpierw wysyła poprawne, a następnie podaje błąd segmentacji (zrzucony rdzeń). Oto część mojego kodu.

    FILE * fp;
int size = 20000;
char sentence[2000]; // the sentence from stdin
if ( argc != 3 ) 
{
    fgets(sentence,sizeof(sentence),stdin); // read from stdin
    fflush(stdin);
    // I think the initialization of word is incorrect, but i do know why it is incorrect
    char *word = strtok (sentence," !\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~\n\t");
    while (word != NULL)
    {
        get_word(word); // get each word
        word = strtok (NULL, " !\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~\n\t");
    }
}
else
{
    fp = fopen( argv[2], "r" );
    if ( fp == 0 )
    {
        printf( "Could not open file\n" );
    }

           char word[1000];
    while (readFile(fp, word, size)) {  // let the program read the file
        get_word(word); // get each word. Works well.
    }
}

funkcja get_word:

void get_word(char *word){
node *ptr = NULL;
node *last = NULL;

if(first == NULL){
    first = add_to_list(word); // add to linked list
    return;
}

ptr = first;
while(ptr != NULL){
    if(strcmp(word, ptr->str) == 0){
        ++ptr->freq;
        return;
    }
    last = ptr;            
    ptr = ptr->next;  
}
last->next = add_to_list(word); // add to linked list

}

Pomóż mi dowiedzieć się, dlaczego otrzymuję błąd segmentacji (zrzucony rdzeń). Program działa na moim Macu, ale nie działa na Linuksie.
Z góry dziękuję.

questionAnswers(2)

yourAnswerToTheQuestion