title: Segmentation fault when using circular doubly linked list of strings
body: <p>I'm trying to write a program that takes as command line arguments: the name of the file to be parsed and a set of delimiters. The program should tokenize the file into substrings delimited by the chars mentioned as argv[2]. Then, I need to add the substrings into a circular doubly linked list and print it in reverse order. The first part works fine, I just need some help regarding the second one (I'm still kind of new to linked lists). My code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX 1024
  
  
  typedef struct node{
      char token[MAX];
      struct node *next;
      struct node *prev;
  }node;
  
  void insert_beginning(node **tail ,char *value)
  {
      node *new_node = malloc(sizeof(node));
      if(new_node == NULL)
      {
          fprintf(stderr, &quot;Error allocating memory\n&quot;);
          exit(EXIT_FAILURE);
      }
  
      strcpy(new_node-&gt;token, value);
      new_node-&gt;prev = NULL;
      new_node-&gt;next = *tail;
      if(*tail == NULL)
      {
          (*tail)-&gt;prev = new_node;
      }
      (*tail)-&gt;prev = new_node;
      *tail = new_node;
  }
  
  void init(node **tail, node **head, char *value) // use this first to initialize the 1st member
  {
      node *new_node = malloc(sizeof(node));
      if(new_node == NULL)
      {
          fprintf(stderr, &quot;Error at memory allocation\n&quot;);
          exit(EXIT_FAILURE);
      }
  
      strcpy(new_node-&gt;token, value);
      new_node-&gt;prev = NULL;
      new_node-&gt;next = NULL;
  
      *tail = new_node;
      *head = new_node;   
  }
  
  void deallocate(node **tail, node **head)
  {
      if(*tail == NULL)
      {
          return;
      }
      node *curr = *tail;
      while(curr-&gt;next != NULL)
      {
          curr = curr-&gt;next;
          free(curr-&gt;prev);
      }
      free(curr);
  
      *tail = NULL;
      *head = NULL;
  }
  
  int main(int argc, char **argv)
  {
      FILE *fin = fopen(argv[1], &quot;r&quot;);
      if(!fin)
      {
          fprintf(stderr, &quot;Could not open file '%s'.\n&quot;, argv[1]);
          exit(EXIT_FAILURE);
      }
  
      if(argc != 3)
      {
          fprintf(stderr, &quot;Invalid format.\nCorrect usage: %s filename.c ''token_list''\n&quot;, argv[0]);
          exit(EXIT_FAILURE);
      }
  
      char buf[MAX];
      char delim[MAX];
      char *fields[MAX];
      strcpy(delim, argv[2]);
  //  size_t len = strlen(delim);
      printf(&quot;%s\n&quot;, delim);
  /*  printf(&quot;The tokens are:\n&quot;);
      for(int i = 0; i &lt; len; i++)
      {
          printf(&quot;%c &quot;, delim[i]);
      }
      putchar('\n');*/
  
      node *tail = NULL;
      node *head = NULL;
  
      while(fgets(buf, MAX, fin))
      {
          buf[strcspn(buf, &quot;\n&quot;)] = '\0';  // strip the trailing newline
          char *token = strtok(buf, delim);
          for (int j = 0; token; ++j)
          {
              fields[j] = token;
              token = strtok(NULL, delim);
              printf(&quot;%s\n&quot;, fields[j]);
          } 
      }
      init(&amp;tail, &amp;head, fields[0]); //  first initialize the 1st element of the list then the rest
      for(int i = 1; *fields; i++)
      {
          insert_beginning(&amp;tail, fields[i]);
      }
      for(node *curr = tail; curr != NULL; curr = curr-&gt;next)
      {
          printf(&quot;%s\n&quot;, curr-&gt;token);
      }    
  
      deallocate(&amp;tail, &amp;head);
      fclose(fin);
      return 0;
  }
  </code></pre>
  <p>Using the file:</p>
  <pre><code>Lorem, ipsum;
   dolor/
  .
  sit/ amet.
  
  
  </code></pre>
  <p>For the command lines: <code>./a.out parse_test.c &quot;;./&quot; </code> , the output is:</p>
  <pre><code>Lorem, ipsum
   dolor
  sit
   amet
  
  </code></pre>
  <p>and then a seg fault. Any idea why? Thank you!</p>