Day 75: Progress with Multi-D Array Pointers

Nothing makes things click better than running some code yourself! Thanks to a great video and a lot of attempts I started to understand how to work with multidimensional array pointers. I am still not fluent but I think I get how to move forward. Shout out to The Dog on Software for his help and great video on the topic. It really helped to give me some perspective on how to approach the concept

TLDR;

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

  • Continued to work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Within that, I continued my work on Tree data structures. Started trying to replicate the example pretty print functions provided in the article I am reading through. I made some progress testing things out with pointers and multidimensional arrays. I took some solid notes and my example code went great for a first step experiment.
  • Reviewed Git commands. You know when you claim you know something and get challenged on that claim, then fail miserably. Yeah that happened to me today. I could have sworn I knew git decently well. I now know I don’t know it nearly as well as I thought LOL.

Working with Multidimensional Arrays Notes

To start we must be aware that when dealing with multidimensional arrays there is no longer any duality between pointers and the array binding/variable themselves. A duality did exist between one-dimensional arrays and pointers (meaning the array binding/variable and a pointer to that array were interchangeable… They both pointed to the first element in the array by default – I think).

For example, a two-dimensional array is stored in a single continuous block of memory. Rather than view this block of memory as a two-dimensional array it is more appropriate to think of it as a one-dimensional array where the value being stored at each element is itself an array. For example, let’s say we have a two-dimensional array of integers. Just like with a one-dimensional array, the array binding/variable automatically points to the memory address for the first element in the array. The difference here is, the element itself is another array. So that address points to the first element in that array.

# include <stdio.h>
# include <stdlib.h>

int main(){

  int singleArr[3] = {2, 3, 6};
  int doubleArr[][3] = { {2, 3, 6}, {4, 5, 8} };

  printf("single array address 1: %d\t\t value 1: %d\n", singleArr, *singleArr);
  printf("double array address 1: %d\t\t value 1: %d\n", (*doubleArr), *(*doubleArr));


  return 0;
}
# Output:

single array address 1: 6422308		 value 1: 2
double array address 1: 6422284		 value 1: 2

The singleArr binding automatically points to the address of the first element in the array. It only needs to be dereferenced once to get the value stored in at that address since it already points to it. This gives us *singleArr = 2

The doubleArr binding automatically points to the address of the first element in the array but that element is an array itself. To access the value of that array instead of the address we must dereference the pointer once. Once it has been dereferenced we get a pointer to the first element of the array that is itself the first element of the doubleArr array. To get the value of this element which corresponds to the common arr[0][0] notation we would once again dereference the pointer. This gives us *(*doubleArr) = 2


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.