title: Put each word of a string into array in C
body: <p>I'm trying to split up a string (typed in by the user at run time) into words (separated by spaces), and put each word into a different slot into an array. So, for example, if I took the string <code>"hello world"</code>, <code>array[0]</code> would contain <code>"hello"</code> and <code>array[1]</code> would contain <code>"world"</code>. And the last slot (in this case <code>array[2]</code>) would contain <code>NULL</code>. Here's what I have so far, which doesn't seem to be working properly. Any help would be appreciated. (By the way, this is part of a program which will call <code>execvp(argv[0],argv);</code> )</p>
  
  <pre><code>char input[100];
  char* argv[20];
  char* token;
  scanf("%s", input);
  
  //get the first token
  token = strtok(input, " ");
  
  int i=0;
  //walk through other tokens
  while( token != NULL ) {
      argv[i] = token;
      i++;
      token = strtok(NULL, " ");
      } 
  argv[i] = NULL; //argv ends with NULL
  </code></pre>