title: Adding to a list of Self Referential Structures in C?
body: <p>I'm running into a host of issues trying to make a self-referential list of structs. I'm currently getting an illegal hardware instruction and all I know is that it's something after my print statement following my entry as it prints. Could anyone help point out what I need to work on to get my implementation correct? Thank you.</p>
  <p>My add function</p>
  <pre><code>Employee *employeeListHead = NULL;;
  
  int nodeCount = 0;
  
  void addEmployee(void)
  {
  
      char *name = (char*) malloc(MAX_NAME_LENGTH*sizeof(char));
      char gender;
      int age;
      char *title = (char*) malloc(MAX_JOB_LENGTH*sizeof(char));;
  
      printf(&quot;Enter name: \n&quot;);   
      scanf(&quot; %100s&quot;, name); 
      scanf(&quot;%*[^\n]%*c&quot;);
  
      printf(&quot;Enter gender: \n&quot;);
      scanf(&quot; %1c&quot;, &amp;gender); 
      scanf(&quot;%*[^\n]%*c&quot;);
  
      printf(&quot;Enter age: \n&quot;);
      scanf(&quot; %d&quot;, &amp;age); 
      scanf(&quot;%*[^\n]%*c&quot;);
  
      printf(&quot;Enter job title: \n&quot;);
      scanf(&quot; %100s&quot;, title); 
      scanf(&quot;%*[^\n]%*c&quot;);
      
      printf(&quot;The employee you've entered is: %s %c %d %s \n&quot;, name, gender, age, title);
  
      Employee *newEmp = (Employee*)malloc(sizeof(Employee));
      
      strcpy(newEmp-&gt;name, name);
      strcpy(newEmp-&gt;gender, &amp;gender);
      newEmp-&gt;age = age;
      strcpy(newEmp-&gt;profession, title);
      newEmp-&gt;next = NULL;
  
      if(employeeListHead == NULL)
      {
          employeeListHead = newEmp;
          nodeCount++;
      }
      else
      {   
          Employee *temp = (Employee*)malloc(sizeof(Employee));
          temp = employeeListHead;
  
          while(temp-&gt;next != NULL){
              temp = temp-&gt;next;
          }
          temp-&gt;next = newEmp;
          nodeCount++;
          free(temp);
      }
          
  }
  </code></pre>
  <p>struct</p>
  <pre><code>typedef struct Employee{
      char name[MAX_NAME_LENGTH];
      char gender[2];
      int age;
      char profession[MAX_JOB_LENGTH];
      struct Employee *next; 
  } Employee;
  </code></pre>