title: Process finished with exit code instead of returning back to main function
body: <p>I am creating an grocery store system as an assignment in C for a class I put off until my senior year. It uses a linked list to keep track of products in the store. I have gotten it mostly working, but for some reason each time I select a menu option, it will run until it tries to access a NULL memory location (Process finished with exit code -1073741819 (0xC0000005)) and exit with some random memory location that it tried to access instead of returning back to the main function. </p>
  
  <p>I have tested out several different while loop conditionals, all of which lead to the process being terminated as it seems like NULL is accessed even if the conditional isn't met.</p>
  
  <p>The program has two classes, both of which are below. I am trying to focus on the "showList" function for now, as I feel like if I'm able to figure one out, then I can correct the rest of them.</p>
  
  <p>showList() is called by option 4 on the menu.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "list.h"
  
  void displayMenu()
  {
  puts("Welcome to the LinkedList grocery store.");
  puts("Please let me know what you want to do by typing in one of the 
  numbers.");
  puts("============================================================");
  puts("1: Add product to store           2: Purchase product from store");
  puts("3: Check price of a product       4: Show products in store");
  puts("5: Remove a product from store        6: Find product");
  puts("7: Inventory       8: Done for today");
  
  }
  
  int main(int argc, char *argv[])
  {
  product * head = NULL, *p;
  char temp[N];
  float sales = 0.0, quantity;
  int choice, done = 0;
  //system("cls");
  
    head = load(head, "inventory.txt");
    while (!done) {
      displayMenu();
      puts("What do you want to do?");
      fflush(stdin);
      scanf("%d", &amp;choice);
      fflush(stdin);
  
      switch (choice)
      {
          case 1:
              head = addProduct(head);
              break;
          case 2:
              fflush(stdin);
              printf("Please enter the name of a product: ");
              fflush(stdin);
              scanf("%s", temp);
              fflush(stdin);
              printf("Please enter the quantity: ");
              fflush(stdin);
              scanf("%f", &amp;quantity);
              fflush(stdin);
              sales += purchase(head, temp, quantity);
              break;
          case 3:
              fflush(stdin);
              printf("Please enter the name of a product: ");
              fflush(stdin);
              scanf("%s", temp);
              fflush(stdin);
              checkPrice(head, temp);
              break;
          case 4:
              showList(head);
              break;
          case 5:
              fflush(stdin);
              printf("Please enter the name of a product: ");
              fflush(stdin);
              scanf("%s", temp);
              fflush(stdin);
              rmItem(head, temp);
              break;
          case 6:
              fflush(stdin);
              printf("Please enter the name of a product: ");
              fflush(stdin);
              scanf("%s", temp);
              fflush(stdin);
              findProduct(head, temp);
              break;
          case 7:
              puts("****INVENTORY****");
              fflush(stdin);
              printf("Total sales: %f\n", sales);
              fflush(stdin);
              display(head);
              break;
          case 8:
              doneToday(head, "inventory.txt");
              done = 1;
              break;
          default:
              puts("Wrong code. Please try again.");
              break;
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>List.c:</p>
  
  <pre><code>#include "list.h"
  
  // load in data from a file appending to the list l, return it
  product * load(product *l, char fn[])
  {
  char name[N], quantity_unit[N], price_unit[N];
  float quantity_value, price_value;
  
  int rt;
  product * head = l;
  FILE * fin = fopen(fn, "r");
  
  if(fin == NULL) {
      printf("InLoad: File open failed (%s)\n", fn);
      return NULL;
  }
  
  while (1) {
      rt = fscanf(fin, "%s %f %s %f %s\n", name, &amp;quantity_value,quantity_unit, 
  &amp;price_value, price_unit );
      if (rt &lt; 5)
          break;
      if (head == NULL)
          head = buildNode(name, quantity_value, quantity_unit, price_value, 
  price_unit);
      else
          append(head, buildNode(name, quantity_value, quantity_unit, 
  price_value, price_unit));
  }
  fclose(fin);
  return head;
  }
  
  void doneToday(product* l, char fn[])
  {
  FILE * fout = fopen(fn, "w");
  if(fout == NULL) {
      printf("InSave: File open failed (%s)\n", fn);
      return;
  }
  product* current = l;
  while(current != NULL){
      fprintf(fout, "%s %f %s %f %s\n", current-&gt;name, current- 
  &gt;quantity_value,current-&gt;quantity_unit, current-&gt;price_value, current- 
  &gt;price_unit);
      current = current-&gt;next;
  }
  
  fclose(fout);
  
  }
  
  // build a product Node
  product * buildNode(char name[], float quantity_value, char quantity_unit[], 
  float price_value, char price_unit[])
  {
  product * p =  (product *) malloc(sizeof(product));
  if(p == NULL) {
      puts("InBuildNode: Memory Allocation Failed!");
      return NULL;
  }
  
  strcpy(p-&gt;name, name);
  p-&gt;quantity_value = quantity_value;
  strcpy(p-&gt;quantity_unit, quantity_unit);
  p-&gt; price_value = price_value;
  strcpy(p-&gt;price_unit, price_unit);
  
  return p;
  }
  
  //shows the current product list
  void showList(product *l)
  {
  product* cursor = l;
  puts("************ Product List ******************");
  printf("Name\tQuantity\tUnit\tPrice\t\tUnit\n\n");
  
  
      while(cursor != NULL){
          printf("%s\t%f\t%s\t%f\t%s\n", cursor-&gt;name, cursor-&gt;quantity_value, 
  cursor-&gt;quantity_unit, cursor-&gt;price_value, cursor-&gt;price_unit);
          cursor = cursor-&gt;next;
      }
  
  
  }
  
  //adds a product to the list if it's not already
  product * addProduct(product* l)
  {
  product* cursor = l;
  product* result = l;
  char name[N];
  float quantity_value;
  char quantity_unit[N];
  float price_value;
  char price_unit[N];
  
  puts("Please enter the name of a product: ");
  scanf("%s", name);
  puts("Please enter the quantity value of a product: ");
  scanf("%f", &amp;quantity_value);
  puts("Please enter the quantity unit of a product: ");
  scanf("%s", quantity_unit);
  puts("Please enter the price value of a product: ");
  scanf("%f", &amp;price_value);
  puts("Please enter the price unit of a product: ");
  scanf("%s", price_unit);
  
  while(cursor != NULL){
      if(name == cursor-&gt;name){
          cursor-&gt;quantity_value += quantity_value;
          printf("The product with name **%s** already exists.", name);
          printf("The quantity was updated. New quantity is: %f", cursor- 
  &gt;quantity_value);
          return result;
  
      }
      cursor = cursor-&gt;next;
  
  }
  
  if(!cursor){
      result = append(l, buildNode(name, quantity_value, quantity_unit, 
  price_value, price_unit));
      puts("New product added successfully!");
  }
  
  return result;
  
  }
  
  product * append(product *l, product * p)
  {
  
  product* cursor = l;
  
  if(cursor == NULL){
      return p;
  
  } else {
  
      while(cursor-&gt;next != NULL){
          cursor = cursor-&gt;next;
      }
  
      cursor-&gt;next = p;
      return l;
      }
  
  }
  
  void checkPrice(product* l, char p[])
  {
  product* cursor = l;
  
  while(cursor-&gt;next != NULL){
      if(strcmp(cursor-&gt;name, p) == 0 ){
          printf("The current price of %s is %f %s", p, cursor-&gt;price_value, 
  cursor-&gt;price_unit);
      }
      cursor = cursor-&gt;next;
      }
  }
  
  void findProduct(product* l, char p[])
  {
  product* cursor = l;
  
  while(cursor-&gt;next != NULL){
      if(strcmp(cursor-&gt;name, p) == 0){
          printf("Product Found: %s %f %s %f %s", cursor-&gt;name, cursor- 
   &gt;quantity_value, cursor-&gt;quantity_unit, cursor-&gt;price_value, cursor- 
   &gt;price_unit);
          return;
      }
      cursor = cursor-&gt;next;
  }
  
  printf("The requested product was not found.");
  }
  
  void display(product *l)
  {
  product* cursor = l;
  
  printf("Product: \n");
      while(cursor){
          printf("%s %f %s %f %s\n", cursor-&gt;name, cursor-&gt;quantity_value, 
  cursor-&gt;quantity_unit, cursor-&gt;price_value, cursor-&gt;price_unit);
          cursor = cursor-&gt;next;
  
      }
  
  }
  
  float purchase(product* l, char p[], float q){
  product* cursor = l;
  float dollars = 0.0;
  
  while(cursor-&gt;next != NULL){
      if(strcmp(cursor-&gt;name, p) == 0){
         if(cursor-&gt;quantity_value &gt; q){
             dollars = cursor-&gt;price_value * q;
             cursor-&gt;quantity_value -= q;
             printf("Sale Completed, dollars made: %f\n", dollars);
             return dollars;
         } else if(cursor-&gt;quantity_value &lt; q){
             dollars = cursor-&gt;price_value * cursor-&gt;quantity_value;
             printf("Sale partially completed, dollars made: %f\n", dollars);
             rmItem(l, cursor);
             return dollars;
         }
      }
  }
  
  printf("The sale was not completed, maybe the product doesn't exist.");
  return dollars;
  
  }
  
  void rmItem(product* l, char p[])
  {
  product * current = l;
  product * previous = current;
  while(current != NULL) {
      if (strcmp(current-&gt;name, p) == 0) {
          if(previous == current)  // the first node
              l = (current-&gt;next);
          else // not the first one
              previous-&gt;next = current-&gt;next;
  
          free (current);
          return;
      }
      previous = current;
      current = current-&gt;next;
      }
  }
  </code></pre>
  
  <p>list.h:</p>
  
  <pre><code>    #ifndef PROJECT3_LIST_H
  #define PROJECT3_LIST_H
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #define N 20
  
  struct product
  {
      char name[N];
      float quantity_value;
      char quantity_unit[N];
      float price_value;
      char price_unit[N];
      struct product *next;
  };
  
  typedef struct product product;
  
  product * load(product *l, char fn[]);
  product * buildNode(char name[], float quantity_value, char quantity_unit[], 
  float price_value, char price_unit[]);
  void showList(product *l);
  product * addProduct(product* l);
  product * append(product *l, product * p);
  void checkPrice(product* l, char p[]);
  void findProduct(product* l, char p[]);
  void display(product *l);
  float purchase(product* l, char product[], float q);
  void rmItem(product* l, char p[]);
  void doneToday(product* l, char fn[]);
  
  
  
  
  #endif //PROJECT3_LIST_H
  </code></pre>