title: How to make a global head* for a LL to be used by all methods
body: <p>I was wondering how can i make a Linked list of structures with a head pointer to be used by other functions.
  I am making a somewhat of a personal malloc and i wanted to make a structure memBlock {.isTaken, .size, .next}</p>
  
  <p>I tried doing static outside of methods and non static as well and i am not sure what i am doing wrong. </p>
  
  <pre><code>static memBlkHdr *head;  
  
  /*
      Traverse LL to find best free block, return null if cant fit
      Can make void, but we can discuss later
  */
  memBlkHdr* bestFit(memBlkHdr *ptr, int size){
  
  
      memBlkHdr *currentptr = ptr;
      memBlkHdr *bestptr = NULL;
  
      while(currentptr != NULL){
          //skip used blks    
          if((*currentptr).isUsed == 1) 
              continue;
          //skip small blks
          if((*currentptr).blkSize &lt; size) 
              continue;
          //By this point its free and fits, set as best if none before
          if(bestptr == NULL) 
              bestptr = ptr;
          //Check if this is better fit than current best
          if(((*currentptr).blkSize - size) &lt; ((*bestptr).blkSize - size)) 
              bestptr = ptr;  
          currentptr = (*currentptr).next;
      }
  
      return bestptr;
  
  }
  
  void* mymalloc(unsigned int size, char *file, int line){  
  
      //local variables
      memBlkHdr *newBlkptr;
      memBlkHdr newBlk;
      //printf("here\n");
  
      //Check for malloc(0)
      if(size == 0){
          fprintf(stderr, "Err: attempt to malloc(0) in FILE: '%s' on LINE: '%d'\n", file, line);
          return NULL;
      }
  
      //Check if blk req size is even, if not, add 1 extra byte for formatting
      if((size &amp; 1) == 1)
          ++size;
  
      /*
      Check if head is NULL(first malloc)
      If yes, initialize, (free, 5000, NULL, point to first memblk of myblock)
      */
      if(head == NULL){
          memBlkHdr newHead = {.isUsed = 0, .blkSize = MAX, .next = NULL, 
          .current = (void*)(&amp;myblock[0])};
          head = &amp;newHead;        
      }
  
      //Find best place to allocate
      newBlkptr = bestFit(head, size);
  
  
  
      //printf("Best fit returned : %d\n", (*newBlkptr).blkSize);
      //No room
      if(newBlkptr == NULL){
          fprintf(stderr, "Err: Not enough free Memory in FILE: '%s' on LINE: '%d'\n", file, line);
          return NULL;    
      }
  
      newBlk = *newBlkptr;
  
  
  
      //ALL OF THIS NEEDS EXCESSIVE TESTING
      //if there is more room in the block;
      if(newBlk.blkSize &gt; size){
          //Make new header for leftover space
          //size current-(what we're using)
          //set .next to cur.next 
          //add size to char arr ptr;
      printf("IsUsed: %d, BlkSize: %d\n", (*head).isUsed , (*head).blkSize);
  
          newBlk.isUsed = 0;
          newBlk.blkSize = newBlk.blkSize - size;
  
          memBlkHdr tmp;
  
          tmp.isUsed = 1;
          //Issue here, changes the head size to this. idk how to fix yet. got stuck
          tmp.blkSize = size;
  
          tmp.next = newBlk.next;
          tmp.current = newBlk.current+(newBlk.blkSize - size); 
  
          newBlk.next = &amp;tmp;
  
      printf("IsUsed: %d, BlkSize: %d\n", (*head).isUsed , (*head).blkSize);
  
  
  
      } 
      else {
          //If perfect fit, just set to used
          //its next is defined already
          newBlk.isUsed = 1;
      }
  
  
      //returns mem adress as void type, pointing to char array
      //to be matched later. 
  
      return  newBlk.current;
  }
  </code></pre>
  
  <p>When i run this, with malloc(500)<br>
  IsUsed: 0, BlkSize: 5000<br>
  IsUsed: 1, BlkSize: 500  </p>
  
  <p>my newBlk should have been pointing to head as its first go, but when i make a tmp, it auto sets its as new head and i am not sure what i am misunderstanding about pointers.</p>
  
  <p>Thank you. </p>