title: Function returning different values inside another function
body: <p>My first function checks for the number of letters that a substring contains from its string.</p>
  <pre><code>int num_matches(char* word, char* letters) {
      char scrabble[128];
      strcpy(scrabble, letters);
      int num = 0;
  
      for (int i = 0; i &lt; strlen(word); i++) {
          if (strchr(letters, word[i]) == NULL) {
              return -1;
          }
  
          for (int j = 0; j &lt; strlen(letters); j++) {
              if (word[i] == letters[j]) {
                  num++;
                  scrabble[j] = '\0';
                  break;
              }
          }
  
      }
      return num;
  }
  </code></pre>
  <p>It returns 4 for &quot;QWOP&quot;, &quot;QWOP&quot;. However, inside the following function it is returning the same incorrect value for every function call, even when buff and letters print as &quot;QWOP&quot;, &quot;QWOP&quot; from my debugging attempt.</p>
  <pre><code>void read_words(int num_count[128], char* (*word_idx)[128], int argc, char** argv) {
      FILE* fp = fopen(argv[1], &quot;r&quot;);
      char* letters = argv[2];
  
      int idx = 0;
      char buff[128];
      int result = 0;
      while (fgets(buff, 128, fp) != NULL) {
          printf(&quot;buff:%s letters:%s\n&quot;, buff, letters);
          result = num_matches(buff, letters);
          printf(&quot;result: %d\n&quot;, result);
          num_count[idx] = result;
          char* word = malloc(strlen(buff) + 1);
          strcpy(word, buff);
          (*word_idx)[idx] = word;
          idx++;
          result = 0;
      }
  
      fclose(fp);
  }
  </code></pre>
  <pre><code>buff:QWOP
   letters:QWOP
  result: -1
  </code></pre>
  <p>My txt file:</p>
  <pre><code>ABC
  DEFG
  QWOP
  QWOP
  QUOKKA
  QUOLL
  QUASH
  QUANDONG
  </code></pre>
  <p>Since fgets stops at a newline and there are no spaces in my text file, I don't think there should be any problems with reading buff.</p>