Day 38: Confusion Declaring Pointers

Continued work on Linked Lists. I tried to write out the improvement in the original program but I got caught on the declarations of the head and tmp variables. I mean based on just looking at them they seem to be pointers but I am just confused to be honest. I mean… Don’t pointers need to point at an existing variable. How can pointers just exist on their own? It’s at this point where I realize how uncomfortable I am with C. That’s okay though. I just have to take things one step at a time.

TLDR;

Okay, so here are the highlights of what I did:

  • Continued work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Continued my study of Linked Lists. I am trying to improve on my first example while noting what I am changing and how the improvements help improve the quality of my code. I got stuck wondering what the declaration of the *head and the *tmp pointers meant. When I first studied pointers it was presented as a data type that holds the address of an existing variable. But in this code there is no existing variable that I am aware of when this is declared. So the question become what is this pointer pointing too????

Node Construction Function

struct node {
  int value;
  struct node* next;
};

// Allows us to write out node_t instead of struct node every time we use that type.
typedef struct node node_t;

node_t *create_new_node(int value){
  node_t *result = malloc(sizeof(node_t));
  result->value = value;
  result->next = NULL;
  return result;
}

int main(){

  node_t *head;
  node_t *tmp;

  // For my personal sanity: Confirm the value of head & tmp.
  // By default we set all nodes from create_new_node function
  // to have a next value of null. The head and tmp pointers
  // literally point to a reserved space for two node structs.

  printf("%p\n", &head);
  printf("%p\n", &tmp);

  tmp = create_new_node(50);
  head = tmp;

  tmp = create_new_node(30);
  tmp->next = head;
  head = tmp;

  tmp = create_new_node(25);
  tmp->next = head;
  head = tmp;

  printList(head);

  return 0;
}
Output:
0061FF2C
0061FF28
25 - 30 - 50 -

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.