title: While loop is overwriting head node of linked list in C
body: <p>So I think what's happening is that my head node keeps being overwritten, but I'm not sure why.  If I remove the while loop and just put in something like this it works just fine.</p>
  
  <pre><code>head = addItem(head, "item one");
  head = addBack(head, "item two");
  
  print(head);
  </code></pre>
  
  <p>Here's the code as it is now, and underneath I'll include the header file with the functions.  Please note all my menu items aren't in the loop yet, have been stuck on getting the linked list right for a while now.  Thanks in advance for any tips or advice.</p>
  
  <p>Main:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include "functions.h"
  
  int menu();
  void print(node *head);
  node* addItem(node *head, char *item);
  node* addBack(node *head, char *item);
  
  
  int main()
  {
      int selection;
      char *item;
      node* head = NULL;
      char junk;
  
      // Run the menu, and do something based on the selection
      do {
          selection = menu();
          // If they choose a number that's not between 1 and 3, or 0
          if (selection &gt; 3) {
              printf("Please select a valid option\n");
          }
          // If they enter 1, add an item to the list
          else if (selection == 1) {
              printf("Enter your list item: ");
              // scanf leftover characters so fgets will work
              scanf("%c", &amp;junk);
              fgets(item, 100, stdin);
              if (head == NULL) {
                  head = addItem(head, item);
              }
              else if (head != NULL) {
                  addBack(head, item);
              }
          }
          else if (selection == 3) {
              // Print remaining items
              print(head);
          }
      } while (selection != 0);
  
  return 0;
  
  }
  </code></pre>
  
  <p>Edit:  Forgot to add the functions in the header file.</p>
  
  <pre><code>    struct node
  {
      char *item;
      struct node *next;
  };
  
  typedef struct node node;
  
  
  // Menu of choices, returns selection
  int menu()
  {
      int selection;
  
      printf("\nChoose an option:\n1: Enter a list item \
      \n2: Delete a list item\n3: Print remaining items \
      \n0: Quit\n\n");
      scanf("%d", &amp;selection);
  
      return selection;
  }
  
  node* addItem(node *head, char *item)
  {
      node *tmp;
  
      tmp = malloc(sizeof(node));
      tmp-&gt;item = item;
      tmp-&gt;next = head;
      head = tmp;
  
  
      return head;
  }
  
  node* addBack(node *head, char *item)
  {
  
      node *tmp, *p;
      tmp = malloc(sizeof(node));
      tmp-&gt;item = item;
      p = head;
      while (p-&gt;next != NULL) {
          p = p-&gt;next;
      }
      p-&gt;next = tmp;
      tmp-&gt;next = NULL;
  
      return head;
  }
  
  void print(node* head)
  {
      node *tmp;
      tmp = head; 
      if (tmp == NULL) {
          printf("Add an item first, list is empty\n");
          exit(0);
      }
      while(tmp != NULL) 
      {
          printf("%s\n ", tmp-&gt;item);
          tmp = tmp-&gt;next;
      }
  }
  </code></pre>