Day 79: Successfully Copied a 2-D Array in C

Solved the practice problem that I was previously stuck on. It took a bit of playing around but now that I know how to work with multidimensional arrays it wasn’t too bad. My first attempt did fail however because I tried to loop through all of a partially used array. The blank slots had some random data that produced crazy characters LOL. Now I can honestly and truly get back to working on my Pretty Print Function with no regrets. I’m ready to GOOOOOO!!!!!

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. Came back around to try and complete the challenge I gave to myself when I first started learning how to work with multidimensional arrays in C. “Copy one 2-D array in another empty 2-D”. It seemed simple enough but I truly struggled with it. So for me to successfully get it done this time around means a lot. Now I can finally finish off my Pretty Print function. I feel confident but some random surprises may pop up again. I hope not though!!
  • I worked through a small section of the git manual. It covered fetching from remote sources and pulling branches from multiple remote repositories. I am trying to slowly go through the manual and watch some more videos because issues keep popping up for me due to my lack of comfort with the tool. To solve this problem I’m going to try and slowly get more comfortable with git. I need to put in my reps and get those sets done!!

Copying Array of Strings Program and Output

/*
 * 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>

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

  // Copy values into new array
  char *c, *d;
  int i = 0;
  while(i < ( sizeof(charString) / sizeof(*charString) )){
    c = *(charString + i);
    d = *(charStringCopy + i);

    while(*c != '\0'){
      *d++ = *c++;
    }

    *d = '\0';
    ++i;
  }

  // Print values from original and copied array
  printf("\n\nArray Contents:\n---------------------\n");
  i=0;
  while(i < ( sizeof(charString) / sizeof(*charString) )){
    c = *(charString + i);
    d = *(charStringCopy + i);

    printf("Address: %d\t\tOriginal String\t%i:\t\t", c, i);
    while(*c != '\0')
      printf("%c", *c++);

    printf("\n");
    printf("Address: %d\t\tCopied String\t%i:\t\t", d, i);
    while(*d != '\0')
      printf("%c", *d++);

    printf("\n");
    ++i;
  }
  // Mission Complete!

  return 0;
}
# Output:

Array Contents:
---------------------
Address: 6422202		Original String	0:		Hello World
Address: 6422112		Copied String	0:		Hello World
Address: 6422232		Original String	1:		My name is Bob
Address: 6422142		Copied String	1:		My name is Bob
Address: 6422262		Original String	2:		Sparta!!!
Address: 6422172		Copied String	2:		Sparta!!!

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.