title: structure with pointer through FIFO pipe in C
body: <p>I am trying to setup a FIFO pipe with one server and one client. </p>
  
  <h3>client.c</h3>
  
  <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;fcntl.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  
  typedef struct buffer {
       int pid;
       char *buffer;
   } my_buffer;
  
  /*
  void removefifo() {
  unlink("fifo_server");
  unlink("fifo_client");
  }
  */
  
  main(int argc, char *argv[]) {
      if (argc != 2) {
      fprintf(stderr, "usa: %s text \n", argv[0]);
      return EXIT_FAILURE;
      }
  
   FILE *file1;
   int fifo_server,fifo_client;
   char *buf_in;
   buf_in = malloc(20*sizeof(char));
  
   //send PID and content
   my_buffer * buf_out;
   buf_out = (my_buffer *)malloc(sizeof(my_buffer));
   buf_out-&gt;pid = getpid();
   buf_out-&gt;buffer = (char *) calloc(20,sizeof(char));
   strcpy(buf_out-&gt;buffer,argv[1]);
  
   fifo_server=open("fifo_server",O_RDWR);
   if(fifo_server &lt; 0) {
    printf("Error in opening file");
    exit(-1);
    }
  
   printf("buffer has PID %d and content %s \n",buf_out-&gt;pid,buf_out-&gt;buffer);
  
  
   write(fifo_server,buf_out,sizeof(struct buffer));
  
   fifo_client=open("fifo_client",O_RDWR);
  
   if(fifo_client &lt; 0) {
    printf("Error in opening file");
    exit(-1);
    }
  
  
   read(fifo_client,buf_in,10*sizeof(char));
   printf("\n * Reply from server: %s * \n",buf_in);
   close(fifo_server);
   close(fifo_client);
  
  }
  </code></pre>
  
  <h3>server.c</h3>
  
  <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;fcntl.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #define FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) //0666
  
  typedef struct buffer {
       int pid;
       char *buffer;
   } my_buffer;
  
  
  
  
  int main()
  {
  int fifo_server,fifo_client;
  char *buf_out;
  int i;
  
  int file1,file2;
  
  my_buffer * buf_in;
  buf_in = (my_buffer *)malloc(sizeof(my_buffer));
  buf_in-&gt;buffer = (char *) calloc(20,sizeof(char));
  buf_out = malloc(20*sizeof(char));
  
  //create fifo-server with p-flag
  file1 = mkfifo("fifo_server",FIFO_MODE); 
  if(file1&lt;0) {
   printf("FIFO_SERVER already exists \n");
   }
  
  fifo_server = open("fifo_server",O_RDWR);
  if(fifo_server&lt;1) {
   printf("Error opening file");
   }
  read(fifo_server,buf_in,sizeof(struct buffer)); //read PID and content
  if(fifo_server&lt;1) {
   printf("Error opening file");
   }
  
  printf("pid %d \n",buf_in-&gt;pid);
  printf("content %s \n",buf_in-&gt;buffer);
  
  //write
  strcpy(buf_out,buf_in-&gt;buffer);
  for (i = 0; buf_in-&gt;buffer[i] != '\0'; i++) {
      buf_out[i] = toupper(buf_in-&gt;buffer[i]);
  }
  
  //create fifo-client with p-flag
  file2 = mkfifo("fifo_client",FIFO_MODE); 
  if(file2&lt;0) {
   printf("FIFO_CLIENT already exists \n");
   }
  
  
  fifo_client = open("fifo_client",O_RDWR);
  write(fifo_client,buf_out,10*sizeof(char)); 
  printf("\n Data sent to client \n");
  
  
  close(fifo_server);
  close(fifo_client);
  
  }
  </code></pre>
  
  <p>I first start the server, then the client.  In particular, when I run the client with </p>
  
  <pre><code>./client "the fox"
  buffer has PID 14491 and content the fox 
  </code></pre>
  
  <p>the server correctly displays the PID but crashes at printing buf_in->buffer. </p>
  
  <pre><code>./server 
  pid 14491 
  Errore di segmentazione
  </code></pre>
  
  <p>Kindly ask for your advice.</p>