Day 80: Returning Arrays from Functions in C

Started working on the function to gather my tree values in a 2-D char array but I didn’t know how to return an array from a function. LOL. Well in this case I guess you could say I really didn’t know how to return a pointer from a function. I had an idea after better understanding 2-D arrays and pointers but I had no experience. Anyways after a bunch of mistakes, notes and failed compiles I got it working in a test scenario.

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 am writing a function that will collect all the values in my tree using a Pre-order traversal. Those collected node values will be organized and stored in a 2-D char array. That way I can parse it correctly later to ensure I get the format output just the way I want. This sounds great but I got stuck at returning that array from the function. I had an idea on how to do it but the syntax is gross! I struggled getting it correct but after a bunch of googling I got it working. I need to write some notes on this and typedefs because they seem to be the key to making this syntax bearable.
  • I read through chapter 7 of the book “Eloquent JavaScript” by Marjin Haverbeke. I am starting to read the book to help cover some insecurities I have about my knowledge of the language. I learned JavaScript on the fly through projects and YouTube videos. There are some core questions about the language that I don’t think I can answer, so hopefully this will help. I walked through the robot program that was presented as a project In Chapter 7. It employed a sort of graph data structure to walk through a postal delivery service performed by a robot. It was really cool but a lot of the ideas went over my head. I am still not that great at data structures so I would need to implement this program before it would really start to click. For now I’m going to move on. I want to come back to the examples once I have covered everything briefly. That way I have some exposure and can start to build connections between topics or features in the language.

Testing returning Arrays Example:

/*
 * This program demonstrates how arrays are
 * returned as values from functions.
 * */

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

typedef int testArr[3];

int (*returnArr())[3]{
  static int arr[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
  printf("\nreturnArr value before return: %d\n", arr);
  return arr;
}

testArr* typedefReturn(){
  static int arr[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
  printf("\ntypedefReturn value before return: %d\n", arr);
  return arr;
}



int main(){
  printf("\nreturnArr value after return: %d\n", returnArr() );
  printf("\ntypeDefReturn value after return: %d\n", typedefReturn() );
  return 0;
}
# Output:

returnArr value before return: 4210720

returnArr value after return: 4210720

typedefReturn value before return: 4210784

typeDefReturn value after return: 4210784

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.