title: Counting word occurrences in a file C
body: <p>Welcome everybody. I am new to Stackoverflow, I code in C for some time.
  I have run to a problem writing a program counting word occurrences in a text file. I need to have an output telling what word occurred how many times. Here is the source code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int new_words=0;
  int nwords=0;
  
  typedef struct element{
      char word[30];
      int how_many;
  } element;
  
  int is_word_new(element ** dictionary, char * string)
  {
      for (int i =0; i&lt;new_words; i++)
      {
          if (strcmp(string, dictionary[i]-&gt;word)==0)
              return 0;
      }
      return 1;
  }
  int which_word(element ** dictionary, char * string)
  {
      for (int i =0; i&lt;new_words; i++)
      {
          if (strcmp(string, dictionary[i]-&gt;word)==0)
              return i;
      }
      return 0;
  }
  
  int main()
  {
      FILE * fp;
      char word[30];
  
  
      fp=fopen("input.txt", "r");
      if (fp==NULL)
      {
          printf("FILE ERROR");
          return 0;
      }
  
  
      while(!feof(fp))
      {
          fscanf(fp, "%s",word);
          nwords++;
      }
      nwords--;
      rewind(fp);
  
      struct element * dictionary = (element*)malloc(sizeof(element)*nwords);
  
      for (int i =0; i&lt;nwords; i ++)
      {
          fscanf(fp, "%s", word);
  
          if( is_word_new(&amp;dictionary, word) )
          {
              strcpy(dictionary[new_words].word, word);
              //dictionary[new_words].word= word;
              dictionary[new_words].how_many=1;
              new_words++;
          }
          else
              dictionary[which_word(&amp;dictionary, word)].how_many++;
          word[0]='\0';
      }
  
      printf("\n\nFinal dictionary\n with %d words", new_words);
      for (int i =0; i&lt;new_words; i++)
      {
          printf("%s %d \n", dictionary[i].word, dictionary[i].how_many);     
      }
  
      free(dictionary);
      fclose(fp);
      return 0;
  }
  </code></pre>
  
  <p>the idea is that i first count how many words are in the text (which somehow is always greater by one than in fact). The function is_word_new checks if a newly read word is already in the dictionary. <code>which_word()</code> tells which word was found</p>
  
  <p>However I get a segmentation fault running this program. 
  When I used the line which is commented <code>// dictionary[i].word=word</code> the program behaved as if there was only "word" in the dictionary.</p>
  
  <p>Please give me hints where am I doing this stuff wrong</p>