title: Structure Link List Segmentation-Fault
body: <p>I just got started on a lab of mine where I calculate the GPA of courses whose information is stored in a linked list of structures. As of now, I'm attempting to just print out all the course information to ensure that they have been properly initialized and added to the link list. </p>
  
  <p>I'm running into a problem though because I keep getting a Segmentation Fault. I understand what the Segmentation Fault means, but I do not know where I am making my mistake. Any help would be appreciated. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX_CLASSES 20
  
  
  /* Function Prototypes */
  struct course * initcourse( int, char *, char *, float, char *, char *);
  void add( struct course * );
  
  /* Definition of a data node holding course information */
    struct course {
      int term;
      char name[15];
      char abbrev[20];
      float hours;
      char grade [4];
      char type[12];
      struct course *next;
    };
  
  
  /* head points to first node in list, end points to last node in list */
  /* initializes both to NULL, no nodes yet */
  struct course *head = (struct course *) NULL;
  struct course *end = (struct course *) NULL;
  
  
  /* Initializes a node, allocates memory for the node, and returns     */
  /* a pointer to the new node. Must pass correct parameters.           */
  struct course * initcourse( int term, char *name, char *abbrev, float hours, char *grade, char *type)
  {
    struct course *ptr;
    ptr = (struct course *) calloc( 1, sizeof(struct course ) );
    if( ptr == NULL )
  
      return (struct course *) NULL;
  
    else
      {
        ptr-&gt;term = term;
        strcpy( ptr-&gt;name, name );
        strcpy( ptr-&gt;abbrev, abbrev );
        ptr-&gt;hours = hours;
        strcpy( ptr-&gt;grade, grade );
        strcpy( ptr-&gt;type, type );
        return ptr;
      }
  }
  
  
  /* This adds a node to the end of the list. You must allocate a node and */
  /* then pass its address to this function                                */ 
  void add(struct course *new)
  {
    if (head == NULL)
      {
        head = new;
      }
    else
      {
        end-&gt;next = new;
        end = new;
      }
  }
  
  /* Prints all information in a node */
  void printnode( struct course *ptr )
  {
    printf("Term -&gt;%d\n", ptr-&gt;term );
    printf("Name -&gt;%s\n", ptr-&gt;name );
    printf("Abbreviation -&gt;%s\n", ptr-&gt;abbrev );
    printf("Hours -&gt;%f\n", ptr-&gt;hours );
    printf("Grade -&gt;%s\n", ptr-&gt;grade );
    printf("Type -&gt;%s\n", ptr-&gt;type );
  }
  
  
  
  
  /* Prints List of Nodes */
  void printlist( struct course *ptr ) 
  { 
    while( ptr != NULL ) 
      { 
        printnode( ptr ); 
        ptr = ptr-&gt;next;  
      } 
  } 
  
  /* Calculates GPA */
  /* float gpa ( struct course *ptr ) */
  /* { */
  /*   float totalhours; */
  /*   float gpa; */
  /*   float gradepoints; */
  
  /*   while (ptr != NULL ) */
  /*     { */
  /*       totalhours += (ptr-&gt;hours); */
  /*       gradepoints = (ptr-&gt;hours * ptr-&gt;grade); */
  /*     } */
  /*   gpa = (gradepoints /ptr-&gt;hours); */
  /* } */
  
  
  
  int main()
  {
  
    int term;
    char name[15];
    char abbrev[20];
    float hours;
    char grade[4];
    char type[12];
    float gpa;
    struct course *ptr;
  
    struct course course1, course2, course3;
  
    course1.term = 1234;
    strcpy(course1.name,"cse1234");
    strcpy(course1.abbrev,"systems");
    course1.hours = 4;
    strcpy(course1.grade,"A");
    strcpy(course1.type,"GEC");
  
  
    ptr = initcourse(course1.term, course1.name, course1.abbrev, course1.hours, course1.grade, course1.type);
  
    struct course *head, *ptr2;
    head = ptr;
    // ptr2 = ptr;
  
    add(ptr);
  
    course2.term = 4332;
    strcpy(course2.name,"cse4332");
    strcpy(course2.abbrev,"Database");
    course2.hours = 4;
    strcpy(course2.grade,"B");
    strcpy(course2.type,"Technical");
  
    ptr2 =  initcourse(course2.term, course2.name, course2.abbrev, course2.hours, course2.grade, course2.type);
  
    add(ptr2);
  
    printlist(head);
  
  
  
  }
  </code></pre>