Day 73: Pointers to Multi-Dimensional Arrays

Got stuck trying to figure out how to correctly use a pointer to access a multi-dimensional array. I found some answers but it seems like there are multiple ways to do it. My array only has two dimensions so after a few compile errors I got it working with a pointing towards the first string in the array (a.k.a char array). So far so good but I have a lot more work to do.

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 continued working on copying a multi-Dimensional char array in C (an array of strings). I got my pointer working but now I need to test stepping to each char array inside the containing array. There are three rows in my case. I guess I could jump to the next element by incrementing the pointer by the size allocated to each row. So… move by 30 bytes I think.
  • I finished reading through chapter 6 of the book “Eloquent JavaScript” by Marjin Haverbeke. The last few sections covered examples that were just going right over my head tbh. I don’t know if that was their intentions but it really made me feel inadequate. BUT, I just think I need to set my bar higher and keep going. Eventually I’ll come back to this chapter and feel like this stuff is easy!! Let’s go!!

Pointing to Multi-dimensional Array Example

/*
 * This program will take an existing array of strings (char arrays) 
 * and copy it into a new array of strings (char arrays).
 *
 * This program will also print out the corresponding 
 * addresses for each character array element (If I can pull that off)
 *
 * */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>    // Add the header file for string functions

int main(){
  char charString[][30] = {
    "Hello World",
    "My name is Bob",
    "Sparta!!!"
  };
  char charStringCopy[3][30];

  char* charPtr;
  char* copyCharPtr;

  charPtr = charString[0];            // Pointer is assigned to the address of the first element in the array
//  copyCharPtr = charStringCopy;    // Pointer is assigned to the address of the first element in the array

  printf("Pointer value: %d\tCharacter: %c", charPtr, *charPtr);

  return 0;
}
# Output:
Pointer value: 6422210  Character: H

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.