title: splitting a long dynamic string into an array of strings in c
body: <p>I'm pretty new to C and can figure out why this function doesn't work consistently whatsoever:</p>
  <pre><code>char **splitString(char *string) {
      char *token = strtok(string, &quot;,&quot;), **finalValue = NULL, **temp = NULL;
      size_t wordIndex = 0;
      while (token != NULL) {
          temp = realloc(finalValue, sizeof(char *));
          if (!temp) {
              freeArray(finalValue);
              finalValue = NULL;
              break;
          }
          temp[wordIndex] = malloc((strlen(token)+1)*sizeof(char));
          if (temp[wordIndex] == NULL) {
              freeArray(finalValue);
              finalValue = NULL;
              break;
          }
          strcpy(temp[wordIndex], token);
          printf(&quot;%s\n&quot;, temp[wordIndex]);
          finalValue = temp;
          printf(&quot;%s\n&quot;, finalValue[wordIndex]);
          wordIndex++;
          token = strtok(NULL, &quot;,&quot;);
      }
      return finalValue;
  }
  </code></pre>
  <p>It receives a string separated by commas and its supposed to split them into different strings, all of which were created via <code>malloc</code>/<code>realloc</code>.</p>