title: Dynamically Creating an Array of Char Pointers in C
body: <p>I am trying to make a function that dynamically adds the pointers of selected words from an input array (allTerms) to an array that it will eventually return (myTerms). The pointers point to various words in the allWords array, and based on a function (isTermNeeded) words are chosen to be included (as pointers to allWords). How do I assign pointers to myTerms and allocate enough space for it to work?</p>
  
  <p>Here is the snippet I'm having trouble with:</p>
  
  <pre><code>myTerms    = (char **)realloc(myTerms, (c+1)*sizeof(char *));
  myTerms[c] = allTerms[i];
  </code></pre>
  
  <p>And here is the full function:</p>
  
  <pre><code>char **getMyTerms(char **allTerms, char info[])
  {
      int     i   =   0,
              c   =   0; // amount of terms selected
  
      char **myTerms; // array of selected terms
  
      while (allTerms[i])
      {
          if (isTermNeeded(allTerms[i], info))
          {
              myTerms     =   (char **)realloc(myTerms, (c+1)*sizeof(char *));
              myTerms[c]  =   &amp;allTerms[i];
  
              c++;
          }
  
          i++;
      }
  
      return myTerms;
  }
  </code></pre>
  
  <p>And here is the warning I've been getting:</p>
  
  <pre><code>term.c:95:15: warning: incompatible pointer types assigning to 'char *' from 'char **'; remove &amp; [-Wincompatible-pointer-types]
                          myTerms[c]      =       &amp;allTerms[i];
                                          ^       ~~~~~~~~~~~~
  1 warning generated.
  </code></pre>