title: Deciding how much memory to dynamically allocate based on user input
body: <p>I need to write a program that does the following:</p>
  
  <ul>
  <li>Prompt the user and ask them how many words they wish to enter</li>
  <li>Allocate dynamic memory using malloc() to store each individual word</li>
  <li>Dynamically allocate another array to store pointers to each of these individual word strings, fill it with the pointers</li>
  <li>Dynamically allocate an additional array for temporary storage of an incoming word</li>
  <li>Prompt the user to enter a string with the same number of words they previously entered.</li>
  <li>Read the users' input into the temporary array one word at a time, once each word has been read, transfer it into one of the dynamically allocated arrays.</li>
  <li>After all of this I should be able to print each word individually.</li>
  </ul>
  
  <p>A sample run could look like this:</p>
  
  <pre><code>How many words do you wish to enter? 5 
  Enter 5 words now:
  I enjoyed doing this exercise
  Here are your words:
  I 
  enjoyed 
  doing 
  this 
  exercise
  </code></pre>
  
  <p>When reading the string, the program should read the word into a temporary array of char, use malloc() to allocate enough storage to hold the word, and store the address in the array of char pointers.How to handle this step?</p>
  
  <p>Here is my question: How can I size my temporary array to be large enough to hold any arbitrary word the user may enter, and then how do I transfer it to the storage array?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  enum { MAX_WORD_SIZE = sizeof("Supercalifragilisticexpialidocious") };
  void resign(char**,int);
  int main()
  {
      char **p;
      int n,i;
      printf("How many words do you wish to enter?");
      scanf("%d",&amp;n);
      p=calloc(n, sizeof(char*));
      puts("Here are your words:");
      resign(p,n);
      for (i=0; i&lt;n; i++) {
          puts(p[i]);
      }
  }
   void resign(char**p,int n)
  {
      int i=0,j=0;
      char c;
      char * temp_word = NULL;
      getchar();
      temp_word = malloc(MAX_WORD_SIZE);
          while ((c=getchar())!='\n') {
          if (c!=' ') temp_word[i++]=c;
          else {temp_word[i]='\0';
          p[j]=temp_word;
          temp_word = malloc(MAX_WORD_SIZE);
              i=0;j++;}
  }
      temp_word[i]='\0';
      p[j]=temp_word;
      free(temp_word);
  }
  </code></pre>