title: Dividing up a char array to different values in struct
body: <p>Through a tcp socket I get a string like this: "SmallTitle fromhost 9595". </p>
  
  <p><strong>Upon reciving it I would like to divide it in to a struct so it's easier to work with.</strong> </p>
  
  <p>This is what I have come up with. But there are problems. 1 it is nasty. 2 When printing the struct in the end i recieve garbage output after the string. See output example at the bottom. Should not printf stop at '\0'? Then it might be missing such.   </p>
  
  <pre><code>struct recieved {
      char what[50];
      char from[50];
      int value;
  };
  
  
  int main(int argc, char const *argv[]) {
      struct recieved leaderRec;
  
      char *word;
      char buf[] = "leadervalue host 9569"; //Example recieved input from socket.
  
      word = (char *) malloc(sizeof(char) * 50);
      int i = 0;
      int count = 1;
  
      do{
          if((buf[i] == ' ') || (buf[i+1] == '\0')){
              //End of word
              //word[strlend(word)+1] = '\0';
              //printf("%s\n", word);
              if(count == 3){
                  //The value is here
                  //get the last number
                  strncat(word,&amp;buf[i], 1);
                  leaderRec.value = atoi(word);
                  //printf("%d\n", value);
              }
  
              if(count == 1)
                  strncpy(leaderRec.what, word, strlen(word));
              else if(count == 2)
                  strncpy(leaderRec.from, word, strlen(word));
              free(word);
              word = (char *) malloc(sizeof(char) * 50);
              count++;
          }
          else {
              //printf("%d\n",i );
              //printf("%s\n", &amp;buf[i]);
              strncat(word,&amp;buf[i], 1);
          }
          //printf("%s", &amp;buf[i]);
          //printf("%d -", i);
          i++;
      } while(buf[i] != '\0');
  
      printf("%s\n", leaderRec.what);
      printf("%s\n", leaderRec.from);
      printf("%d\n", leaderRec.value);
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>leadervalue
  host__vdso_get
  9569
  </code></pre>
  
  <p>Any better ways to do this?
  How would I avoid the garbage output at the second print in the example output?</p>