Day 69: Array Pointers & step by Memory Size

I started on the tree and immediately got stuck because I wasn’t sure how to return an array from a function, or how to use a pointer with an array. I clearly need that to keep my functions relatively pure. I don’t want to be running functions that edit global variables so I think it’s worth it for me to practice this now. I built a practice program but I still want to try running a program that prints out each character with it’s corresponding address, just so that I can see it with my own eyes.

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 copied a practice program for working with pointers and character arrays (a.k.a strings). I have shared the code below. I wonder what \0 is equivalent to. Does it equal null0false and more or just one of those values????
  • Started reading up on the Object-Oriented Programming Methodologies concept called Polymorphism. I didn’t get far in it today due to time constraints.

C Copy string Program

/*
 * This program will take an existing string (char array) 
 * and copy it into a new string (char array).
 *
 * */

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

int main(){
  // declare and initialize the first string
  char charString[] = "Hello World";
  // declare the size of the new string we will copy into
  char charStringCopy[12];

  // Declare pointers we will use for each char array respectively
  char* charPtr;
  char* copyCharPtr;

  charPtr = charString;            // 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("String value pointed by pointer is: ");

  while(*charPtr != '\0'){
    printf("%c", *charPtr);        // Displays the value pointed at by the pointer.
    *copyCharPtr++ = *charPtr++;    // Copies the value pointed at by charPtr into address pointed at by copyCharPtr
                                    // Then increments the address stored in each pointer by one to access the next element in the string
  }

  *copyCharPtr = '\0';
  printf("\n\nNew copied string pointer is: ");
  puts(charStringCopy);

  return 0;
}
# Output

String value pointed by pointer is: Hello World

New copied string pointer is: Hello World

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.