title: Segmentation fault (core dumped) read from stdin
body: <p>I am trying to count the number of each word in a file. The file can be either stdin or a filename provided on the command line(./count -f ). So far the program gives the correct outputs when reading a file from command line. But an error happens when i am trying to read from stdin. The program first output the correct, then give a Segmentation fault (core dumped). Here is part of my code. </p>
  
  <pre><code>    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," !\"#$%&amp;'()*+,./:;&lt;=&gt;?@[\\]^_`{|}~\n\t");
      while (word != NULL)
      {
          get_word(word); // get each word
          word = strtok (NULL, " !\"#$%&amp;'()*+,./:;&lt;=&gt;?@[\\]^_`{|}~\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.
      }
  }
  </code></pre>
  
  <p>get_word function:</p>
  
  <pre><code>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-&gt;str) == 0){
          ++ptr-&gt;freq;
          return;
      }
      last = ptr;            
      ptr = ptr-&gt;next;  
  }
  last-&gt;next = add_to_list(word); // add to linked list
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me figure out why i get a segmentation fault(core dumped).
  The program works on my mac, but does not work on Linux.<br>
  Thanks in advance.</p>