title: Parsing variable and segmentation fault (core dumped) error
body: <p>I am having an issue with getting a user string passed in as a variable parsed in order to store each seperate word in it's own variable. Below is my attempt at this.</p>
  
  <p>There is another part of the program in <code>client.c</code> that is requesting user input. Once I type in a string in the client "foo 123 anwhere blah" it gets passed into the server program below as the variable <code>buf</code>. Once I have it I make a copy of it into <code>temp</code>, so I don't damage the original string (at least at this point in testing). Then I run the <code>strctok</code> against the variable trying to parse out each word.</p>
  
  <p>If I'm even on the right track at the moment it will compile with no errors and run. However when I send data to it from the client a "segmentation fault (core dumped)" appears.</p>
  
  <p>Can anyone shed any light as to why?</p>
  
  <pre><code>        /*
   * server.c
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;strings.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt; 
  #include &lt;netdb.h&gt;
  #include &lt;cstring&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  #define SERVER_PORT 1617 
  #define MAX_PENDING 5
  #define MAX_LINE 256
  
  int main(int argc, char **argv) {
  
  struct sockaddr_in sin;
  socklen_t addrlen;
  char buf[MAX_LINE];
  int len;
  int s;
  int new_s;
  char *temp;
  char fname[32], lname[32], city[32], zip[32], country[32];
  
  /* build address data structure */
  bzero((char *)&amp;sin, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons (SERVER_PORT);
  
  /* setup passive open */
  if (( s = socket (AF_INET, SOCK_STREAM, 0)) &lt; 0) {
      perror("socket");
      exit(1);
  }
  
  if ((bind(s, (struct sockaddr *) &amp;sin, sizeof(sin))) &lt; 0) {
      perror("bind");
      exit(1);
  }
  
  listen (s, MAX_PENDING);
  
  addrlen = sizeof(sin);
  cout &lt;&lt; "The server is up, waiting for connection" &lt;&lt; endl;
  
  /* wait for connection, then receive and print text */
  while (1) {
      if ((new_s = accept(s, (struct sockaddr *)&amp;sin, &amp;addrlen)) &lt; 0) {
          perror("accept");
          exit(1);
      }
      cout &lt;&lt; "new connection from " &lt;&lt; inet_ntoa(sin.sin_addr) &lt;&lt; endl;
  
      while (len = recv(new_s, buf, sizeof(buf), 0)) {
  
          temp = buf;
          strcpy(fname, strtok(buf , " "));
              strcpy(lname, strtok(NULL, " "));
              strcpy(city , strtok(NULL, " "));
              strcpy(zip , strtok(NULL, " "));
              strcpy(country, strtok(NULL, " "));
  
  
              printf("%s\n", fname);
              printf("%s\n", lname);
              printf("%s\n", city);
              printf("%s\n", zip);
              printf("%s\n", country);
  
  
          /* send (new_s, temp, strlen(temp) + 1, 0); */
      }
  
      close(new_s);
  }
  }                      
  </code></pre>
  
  <p>As requested the -g option on gcc:</p>
  
  <pre class="lang-none prettyprint-override"><code>server.c:6:20: error: iostream: No such file or directory
  
  server.c:13:19: error: cstring: No such file or directory
  
  server.c:14:19: error: cstdlib: No such file or directory
  
  server.c:16: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before ânamespaceâ
  
  server.c: In function âmainâ:
  
  server.c:42: warning: incompatible implicit declaration of built-in function âexitâ
  
  server.c:47: warning: incompatible implicit declaration of built-in function âexitâ
  
  server.c:53: error: âcoutâ undeclared (first use in this function)
  
  server.c:53: error: (Each undeclared identifier is reported only once
  
  server.c:53: error: for each function it appears in.)
  
  server.c:53: error: âendlâ undeclared (first use in this function)
  
  server.c:59: warning: incompatible implicit declaration of built-in function âexitâ
  
  server.c:65: warning: incompatible implicit declaration of built-in function âmemcpyâ
  
  server.c:66: warning: incompatible implicit declaration of built-in function âstrcpyâ
  
  server.c:66: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
  
  server.c:66: note: expected âconst char *â but argument is of type âintâ
  
  server.c:67: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
  
  server.c:67: note: expected âconst char *â but argument is of type âintâ
  
  server.c:68: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
  
  server.c:68: note: expected âconst char *â but argument is of type âintâ
  
  server.c:69: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
  
  server.c:69: note: expected âconst char *â but argument is of type âintâ
  
  server.c:70: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
  
  server.c:70: note: expected âconst char *â but argument is of type âintâ
  </code></pre>