title: Splitting C char array into words
body: <p>I am trying to split a given char array into separate strings. I am doing this by putting the address of each word into an array, and then getting the string from the address to print.</p>
  
  <p>So I have updated my code but now the program freezes after printing the numArgs but before "test2." I don't understand why.</p>
  
  <pre><code>----------------old code-----------------------   
  char* parseArgs(char* comPtr){
      char *args[100] = {0};
      char *token;
      int i = 0;
      token = strtok(comPtr, " ");
      while(token != NULL){
          args[i] = malloc(100);
          args[i] = &amp;token;
          token = strtok(NULL, " ");
      }
      return *args;
  }
  
  char* args = parseArgs(comPtr);
  int i = 0;
  while(i &lt; numArgs){
      printf("arg%d: %s\n",i,&amp;args[i]);
      i++;
  }
  -----------------------end old code--------------------
  
  ------------------new code------------------------
  int countArgs(char* comPtr){
      char *token;
      int i = 0;
      token = strtok(comPtr, " ");
      while(token != NULL){
          i++;
          token = strtok(NULL, " ");
      }
      return i;
  }
  
  char** parseArgs(char* comPtr){
      printf("test1");
      char** args = calloc(100, sizeof(char*));
      char* token;
      int i = 0;
      while(token = strtok(comPtr, " ")){
          args[i] = token;
      }
      printf("test2");
      return args;
  }
  
  printf("ComPtr: %s\n",comPtr);
  char* path = "/bin/";
  //int pid = fork(); //pid always 0 so using pid = 1 to test
  //printf("PID:%d",pid);
  int pid = 1;
  printf("PID:%d",pid);
  if(pid != 0){
      int numArgs = countArgs(comPtr);
      printf("test1");
      printf("NumArgs: %d\n",numArgs);
      printf("test2");
      char** args = parseArgs(comPtr);
      int i = 0;
      printf("test3");
      while(i &lt; numArgs){
          printf("arg%d: %s\n",i,args[i]);
          printf("test4");
          i++;
      }
  }
  else{
      //waitpid();
  }
  </code></pre>