title: C Linked lists with global variables
body: <p>Hi I'm new to C and I'm struggling to get a linked list working I'm having to use global variables because I can't change the parameters of the function that is being called. I declare the struct and two global pointers to keep track of the root address and last address like this.</p>
  
  <pre><code>struct node {
    char* pointer;
    struct node *next;
  };
  
  struct node** rootNode;
  struct node** lastNode;
  </code></pre>
  
  <p>Then inside the function I malloc a new struct and try to setup the first node in the list. To try and save the location of this node I think I'm assigning the global pointer lastNode to the pointer of root.   </p>
  
  <pre><code>struct node *root = malloc(sizeof(struct node));
  ...
  root-&gt;pointer=ptr;
  root-&gt;next = 0;
      rootNode = &amp;root;
  lastNode = &amp;root; 
  </code></pre>
  
  <p>Then I try to add aditional nodes by mallocing another node and then linking the previous node using the address stored in my lastNode pointer.  </p>
  
  <pre><code>struct node *newNode = malloc(sizeof(struct node));
  
  newNode-&gt;pointer=ptr ;
  (*lastNode)-&gt;next = newNode;
  newNode-&gt;next = 0;
  lastNode = &amp;newNode;
  </code></pre>
  
  <p>However this doesn't really seem to work. When I run the following the program matches the first two items in the list but then returns null for all nodes after that. I've been stuck on this for 2 days now and any help would really be appreciated :)</p>
  
  <pre><code>struct node* test;
  test = (*rootNode);
  enter code here
  while (test) {
         if (test-&gt;pointer == ptr) {
         printf("MATCH!!");
         notFound = 0;
         break;
         }
      else {
      test = test-&gt;next;
      }
  }
  </code></pre>
  
  <p><em>EDIT</em> A couple of people have asked me to supply some more code. This is the function in which I would like to create the linked list. It's called multiple times while my program is running and I'm trying to add a new node to my linked list every time it gets called. I've also included the global variables at the top.</p>
  
  <pre><code>struct node** rootNode;
  struct node** lastNode;
  int firstRun = 1;
  
  struct node {
  char* pointer;
  struct node *next;
  };
  
  void malloc(size_t sz) {
  
  size_t maxSize = (size_t)-1;  
  char * payloadPtr = NULL;
  
  if (sz &gt; maxSize - sizeof(struct node)+sizeof(int)) {
  return ptr;
    }
  
    if (firstRun) {
  
  struct node *root = malloc(sizeof(struct node));
  
  ptr = malloc(sizeof(size_t)+sz);
  
  if (ptr == NULL) {
      return ptr;
      }
  memcpy(ptr, &amp;sz, sizeof(int));
  payloadPtr = ptr+1;
  root-&gt;pointer=payloadPtr; 
  root-&gt;next = 0;
  rootNode = &amp;root;
  lastNode = &amp;root;
  firstRun = 0;
  
    }
    else { 
  struct node *newNode = malloc(sizeof(struct node));
  ptr = malloc(sizeof(size_t)+sz);
  
  if (ptr == NULL) {
    return ptr;
  }
  memcpy(ptr, &amp;sz, sizeof(int));
  payloadPtr =ptr+1;
  newNode-&gt;pointer= payloadPtr; 
  (*lastNode)-&gt;next = newNode;
  newNode-&gt;next = 0;
  lastNode = &amp;newNode;                
  }
  
  return payloadPtr;
  }
  </code></pre>