title: Reading and writing a structure to a pipe in C
body: <p>I'm attempting to write a structure consisting of a an array of characters, an integer value, and a pointer to a pipe. The structure represents a node in a singly linked list.</p>
  
  <pre><code>//Define a linked-list node object
  typedef struct node{
      char word[128];
      int frequency;
      struct node *next;
  } NODE;
  </code></pre>
  
  <p>The object of the program is to use a pipe to pass a node from several concurrent child processes to the parent. The pipe structure I've implemented seems to work fine with regular character arrays but won't work if I try to pass the entire node object.</p>
  
  <pre><code>    //for each file argument, create a child process
          for (i = 1; i &lt; argc; i ++)
          {
              pipe(p[i-1]);
              pid = fork();
  
              if (pid == 0)
              {
                  //child process
                  close(p[i-1][0]);
                  NODE *tmp;
                  NODE *out = freqCheck(argv[i], tmp);
                  write(p[i-1][1], out, sizeof(NODE));
                  exit(0);
              }
          }
  if (pid &gt; 0){
                  //parent process
                  int j;
                  for (j = 0; j &lt; argc-1; j++)
                  {
                      close(p[j][1]);
                      NODE *tmp;
                      read(p[j][0], tmp, sizeof(NODE));
  
                      printf("%s\n", tmp-&gt;word);
                  }
  
  
              }
  </code></pre>
  
  <p>When the parent process attempts to read from the pipe and interpret one of the attributes of the structure, I just get null values back.</p>
  
  <p>I'm using an array of 2 element integer arrays to keep track of each child processes' pipe. The printf statement above returns null revery time.</p>
  
  <p>Any idea what I'm doing wrong?</p>
  
  <p>Code for the FrqCheck method:</p>
  
  <pre><code>//Method for determining the most occuring word
  NODE * freqCheck(char *file, NODE *max)
  {
      int i; 
      FILE *f;
      char str[128];
  
      //Set up head and tail nodes
      NODE *head = NULL;
      NODE *tail = NULL;
  
      if ((f = fopen(file, "r")) == NULL)
          {
              //sprintf(output, "%s could not be opened.", file);
          }else
          {
              //scan each word of the input file
              while(fscanf(f, "%s ", str) != EOF)
              {
                  //if the linked-list has no nodes, create one
                  if (head == NULL)
                  {
                      NODE *n;
                      n = (NODE *)malloc(sizeof(NODE));
                      strcpy(n-&gt;word, str);
                      n-&gt;frequency = 1;
                      n-&gt;next = NULL;
                      head = n;
                      tail = n;
  
                  }else{  //search the linked list for the found word.
  
                      NODE *current = head;
                      int found = 0;
  
                      while((current != NULL) &amp;&amp; (found == 0))
                      {
                          //if the word is found increment the frequency
                          if (strcmp(current-&gt;word, str) == 0)
                          {
                              current-&gt;frequency ++;
                              found = 1;
                          }else
                          {
                              current = current-&gt;next;
                          }
                      }
  
                      //if the word is not found, create a node and add to the liked-list
                      if (found == 0)
                      {
                          NODE *new;
                          new = (NODE *)malloc(sizeof(NODE));
                          strcpy(new-&gt;word, str);
                          new-&gt;frequency = 1;
                          new-&gt;next = NULL;
                          tail-&gt;next = new;
                          tail = new;
                      }
                  }
              }
              //traverse the linked-list and find the word with the maximum frequency
              NODE *tmp = head;
              max = tmp;
  
              while (tmp != NULL)
              {
                  if (tmp-&gt;frequency &gt; max-&gt;frequency)
                  {
                      max = tmp;
                  }else
                  {
                      tmp = tmp-&gt;next;
                  }
              }
              //sprintf(output, "%s %s %d", file, max-&gt;word, max-&gt;frequency);
          }
      //printf("%s\n", max-&gt;word);
      return max;
  }
  </code></pre>