title: C - Sending struct array over pipe
body: <p>I'm trying to send an array of structs through a pipe. I have a struct:</p>
  
  <pre><code>typedef struct visitordata {
      char name[80];
      char email[80];
      int id;
      char reg_time[9];
  }visitordata;
  </code></pre>
  
  <p>Then I do</p>
  
  <pre><code>//...
  //ds is the number of struct entries to be stored
  visitordata* V;
  V = (visitordata*)malloc(ds * sizeof(visitordata));
  
  pid_t child = fork();
  if(child &lt; 0) {
      perror("Fork error");
          exit(1);
      }
   else if (child &gt; 0) { //parent process
      write(pipefd[1], &amp;V, sizeof(V));
      close(pipefd[1]);
      fflush(NULL);
      pause();
  
      sleep(1);
      pause();
      kill(child,SIGTERM);                
      waitpid(child, &amp;status, 0);
  }
  else { //child process
      visitordata* data;
      close(pipefd[1]);
  
      read(pipefd[0], &amp;data, sizeof(data));
      close(pipefd[0]);
      flush(NULL);
      for (i = 0; i &lt; ds; ++i) {
          printf("Received: %s\r\n", data[i].name);
      }
  
      kill(getppid(), SIGUSR1);
      pause();                
  }
  </code></pre>
  
  <p>But then, if I have two rows for input, the Received part only prints out one row with the string "free" (which I think comed from the username on the server: <code>freeze@server:</code>) and then <code>Received:</code> without anything else. What am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Updated the code as per suggestions:</p>
  
  <pre><code>visitordata* V;
  V = (visitordata*)malloc(ds * sizeof(visitordata));
  
  pid_t child = fork();
  if(child &lt; 0) {
      perror("Fork error");
          exit(1);
      }
   else if (child &gt; 0) { //parent process
      write(pipefd[1], V, ds * sizeof(V));
      close(pipefd[1]);
      fflush(NULL);
      pause();
  
      sleep(1);
      pause();
      kill(child,SIGTERM);                
      waitpid(child, &amp;status, 0);
  }
  else { //child process
      visitordata* data;
      data = (visitordata*)malloc(ds * sizeof(visitordata));
      close(pipefd[1]);
  
      read(pipefd[0], data, ds * sizeof(data));
      close(pipefd[0]);
      flush(NULL);
      for (i = 0; i &lt; ds; ++i) {
          printf("Received: %s\r\n", data[i].name);
      }
  
      kill(getppid(), SIGUSR1);
      pause();                
  }
  </code></pre>
  
  <p>Now my output is like this:</p>
  
  <pre><code>Received: 0▒:▒0▒:▒ 
  Received:
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Updated </p>
  
  <pre><code>read(pipefd[0], data, ds * sizeof(data));
  </code></pre>
  
  <p>and </p>
  
  <pre><code>write(pipefd[1], V, ds * sizeof(V));
  </code></pre>
  
  <p>to</p>
  
  <pre><code>read(pipefd[0], data, ds * sizeof(visitordata));
  </code></pre>
  
  <p>and </p>
  
  <pre><code>write(pipefd[1], V, ds * sizeof(visitordata));
  </code></pre>
  
  <p>Now my output is:</p>
  
  <pre><code>Received: 0Tɢ0Tɢ
  Received:
  </code></pre>