It's been a month, at least by ALX calendar it's been four weeks since I last made a writeup on my experiences. As quickly as this period passed by, I had a lot of notable experiences I would like to share.
The first week started with a deeper dive into pointers. We already started studying pointers since the previous month and even had two projects on it. It wasn't long before I realized that this concept deserved every attention it was getting; Yea a great grasp of this concept was needed to succeed in future projects. Without further ado, we covered recursions, creating our static libraries and working with command line arguments(argc & argv); all in the same week along with having to complete brain draining tasks on every concept learned all within twenty four hours. I had especially been looking forward to recursion so I'll give it some attention. Recursion is simply a concept where a function calls itself. I believe it can be used to achieve basically everything a while loop can achieve and most cases makes codes look neater. One important concept to emphasize when working with recursions is to provide a base/exit condition. You definitely don't want to end up in an infinite look Let's look at an example of a function to find the factorial of numbers using recursion.
int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
The logic behind this function factorial is to keep multiplying the argument by it's successive decrements of one until it decrements to one; literally how factorials work e.g 5! = 5 (5-1) (4 - 1) (3-1) (2-1). However, the first thing we do is provide a base case to exit, i.e. if (n <= 1) return 1;. If we had skipped this first step, we would have been stuck in a loop on constant multiplication and decrementing. It would reach n factorial(0), n factorial(-1) and continued that sequence. Another important concept of recursion is that it uses the stack and the principle of FILO (First In Last Out) as opposed to normal queues with FIFO. So taking case study of factorial(5). Even though this is called first, what it does is keep calling the function and decrementing 5 until it reaches factorial(1) and executes the base case first(returns 1). It then executes the most recently called functions in that order before finally executing factorial(5). This is also why you would rather arrange your clothes in queues rather than stacks lol.
The second week was majorly on dynamic allocation and freeing of memory. You see why I said pointers deserved the attention it was getting. Dynamic memory allocation would be impossible with arrays. The importance of dynamic allocation can be emphasized. There are times where we realize, Oh I need an extra space for additional elements after already writing long lines of codes. If we were using static memory e.g an array, we would have to start looking through long lines of code to find where we initially declared the array. Even if we successfully found it and increased the size, adding an extra element might affect the index of previous elements. We can save ourselves this stress and a lot of stress by simply calling malloc from the standard library . Malloc takes as an argument, the number of bytes needed andreturns a pointer to the allocated memory so we have access to it e.g malloc(10) would allocate 10 bytes. Remember to check if the returned pointer is null and free every allocated memory once you're through.
For the third week, we treated C preprocessors, working with structures and using the typedef keyword, working with with function pointers as well as variadic functions. I particularly find these topics interesting, we've been using preprocessors in our codes before this but we got to understand more on their significance. The freedom to define our own data types using structures is interesting too and calling other functions outside main via function pointers is fascinating. You get to appreciate the essence of structures and function pointers when used together; especially when working with variadic functions too. Variadic functions are functions that accept a variable number of arguments. The arguments can also be of different data types plus this is also responsible for the amazing functionality of the printf function. Finding these concepts interesting though doesn't mean in any way that they were easy to grasp and understanding them was merely a step to solving the tasks.
The concepts learned in the previous week were to be applied in the fourth week and here we had to make our own printf function. It was a team project but that didn't exactly reduce the complexity. It did help tho; I hate to imagine doing that project alone. It was a project one would find almost impossible if the previous week's concepts weren't thoroughly understood. It starts to seem easier when you do; however implementing some other behaviors of the standard printf function complicates things. I had an amazing partner though, so we worked together and scaled through. Yea, I couldn't have asked for a better teammate. On completing the printf project, we barely had time to relax and dived into linked lists. With linked lists, we can continuously add data and dynamically allocate memory for additional data without having to worry about previously allocated memory, unlike arrays. We just have to keep track of pointers to the addresses where theses data are stored in memory.
I'm excited to have completed another month in this program and I've really learnt a lot in this period. Honestly, I have a lot of feelings to convey but I'll stop here.