title: Trying to make linked list, getting seg fault
body: <p>New to C, but I'm trying to write this Linked List program, and I keep getting Segmentation fault: 11. I think I've narrowed it down to the problem being just in the linked list creation -- I marked it as the problem area. When I comment this section out, the seg fault doesn't happen.</p>
  
  <p>I keep going over what is happening on paper and I can't understand why it won't work. Perhaps I just misunderstand the use of pointers or malloc since I'm inexperienced.</p>
  
  <p>example text file that this program should work with:</p>
  
  <pre><code>&gt;984932:39284 mus musculus okodvcxvmkw2e2p \n
  ATCTCAATCGCACTATCTAGCATGTCGTATGCTTGCATGTCGTAGTCGT\n
  ATGCTATGCTTACTCTATTTACGGCGCATCGTGATCGTAGGAGCGAGCT\n
  
  &gt;984932:39284 mus huumoros okodvcxvmkw2e2p \n
  ATCTCAATCGCACTATCTAGCATGTCGTATGCTTGCATGTCGTAGTCGT\n
  ATGCTATGCTTACTCTATTTACGGCGCATCGTGATCGTAGGAGCGAGCT\n
  
  &gt;984932:39284 mus pisces okodvcxvmkw2e2p \n
  ATCTCAATCGCACTATCTAGCATGTCGTATGCTTGCATGTCGTAGTCGT\n
  ATGCTATGCTTACTCTATTTACGGCGCATCGTGATCGTAGGAGCGAGCT\n
  </code></pre>
  
  <p>What I'm trying to do:
  Create a linked list, where each node is one block of the text above. That is, each node contains the header which starts with '>', and the sequence data that is all of the ACTG. In the above example text file, there would be 3 nodes in addition to the head/tail nodes in the list.</p>
  
  <p>How I'm trying to do it (the problem section):
  Char is scanned. If char is '>', then we know we're at the header, and we read all following chars into the new node's header field until we reach the newline char. At this point, we know we're going to read in sequence data. Continue to do so until we reach another '>', and when we do, repeat.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int list_header_size = 200;
  int list_data_size = 2000;
  
  struct list{
      char *header;
      char *data;
      struct list *next;
      struct list *prev;
  };
  
  //append char onto a string
  void append(char *s, char c){
      int len = strlen(s);
      s[len] = c;
      s[len + 1] = '\0';
  }
  
  int create_list(char *filename){
  
      FILE *fp = fopen(filename, "r");
  
      if(fp == NULL){
          printf("File could not be opened. Exiting..");
          exit(1);
      }
  
      //setup head - doesn't hold a char
      struct list *head = malloc(sizeof(struct list));
      head-&gt;next = NULL;
      head-&gt;header = NULL;
      head-&gt;data = NULL;
      head-&gt;prev = NULL;
  
      //setup tail - doesn't hold a char
      struct list *tail = malloc(sizeof(struct list));
      tail-&gt;next = NULL;
      tail-&gt;header = NULL;
      tail-&gt;data = NULL;
      tail-&gt;prev = NULL;
  
      /***scan the .fasta file, populate list***/
  
      //char holder
      char c;
      int list_size = 0;
      int i = 1;
  
      //pull single char from file until end of file is reached
      do{
  
          c = getc(fp);
  
          //******PROBLEM IS IN THIS SECTION********//
  
          //if header text is found
          if(c == '&gt;'){
  
              //create a node
              struct list *temp = malloc(sizeof(struct list));
  
              //first case to setup head
              if(i == 1){
                  head-&gt;next = temp;
                  temp-&gt;prev = head;
                  i = 0;
              }
  
              tail-&gt;next = temp;
              tail-&gt;prev = temp;
  
              //create space for header/sequence data in the new node
              temp-&gt;header = (char*) malloc(sizeof(list_header_size));
              temp-&gt;data = (char*) malloc(sizeof(list_sequence_size));
  
              //add current char to header
              append(temp-&gt;header, c);
  
              c = getc(fp);
  
              //put file's header data into node's header data
              while(c != '\n'){
                  append(temp-&gt;header, c);
  
                  c = getc(fp);
              }
  
              //put file's sequence data into node's sequence data
              while(c != '&gt;' &amp;&amp; c != EOF){
                  append(temp-&gt;data, c);
              }
          }
  
          //*******END OF PROBLEM SECTION********//
  
      }while(c != EOF);
  
      /***end of scanning .fasta file and creating linked list***/
  
      return 1;
  }
  
  int main(int argc, char * argv[]){
  
      char *filename = (char*) malloc(80);
  
      //check options
      int i;
      for(i = 1; i &lt; argc; i++){
  
          if(argv[i][0] == '-'){
  
              switch(argv[i][1]){     
                  default:;
              }
  
          }else{
              //arg is filename
              filename = argv[i];
          }
  
      }
  
      create_list(filename);
  
      return 1;
  }
  </code></pre>