Hi guys,


I wanted to share an exercise from Zeds LCTHW exercise 16. Im proving not be a natural to coding so Im reaching out today for some assistance. Zed has explained to go through the script and write a comment line above every single line saying what it does in English. I was hoping someone wouldn't mind looking over this for me in and correct comments which don't have the right description. This should allow me to trace through each function call and variable to understand where it comes from in the program.


After these two processes I wanted to be able to comprehend why the tools in this script/exercise are important, because I feel once I can visualise more of a context for the methods Im getting shown I should be able to understand them fully!


Thank you very much

Code:
// here get into dynamically allocated memory


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


struct Person { //if you come from OOP the closest thing you have to the struct right here is an object. C++ is closer to structs.: Struct creates a new type that is an amalgamation of other types. 
	int age;
	char *name;
	int height;
	int weight;
};
//Heres the first function that creates the structure above.
struct Person *Person_create(char *name, int age, int height, int weight)
{
//struct Person is assigned to a the pointer *who. This is assigned to malloc which is passed to sizeof which calculates the total size of the struct, given all the fields inside it.
	struct Person *who = malloc(sizeof(struct Person));
	//assert is basically checking that malloc didn't return a NULL invalid pointer. "macro"
	assert(who != NULL);
//each field of struct person is initialised using -> syntax, to say what part of the struct I want to set. strdup function is used to to duplicate the string for the name. Its makes sure that this structure owns it. strdup copies the original string into the memory it creates.
	who->name = strdup(name);//the duplicates made so it can be "destroyed later and not have to worry about weather this string
	who->age = age;
	who->height = height;
	who->weight = weight;
//Not sure why return is set to who. does this initialise each element?
	return who;
}
// A void type set to Person_destroy with the argument struct Person *who
void Person_destroy(struct Person *who)
{
//assert is checking that something didnt return a NULL invalid pointer.
	assert(who != NULL);
//free function is deallocating the momory from name and from who.
	free(who->name);
	free(who);
}
//The person_print function gets the field from the struct to print it. Its uses the same x->y syntax.
void Person_print (struct Person *who)
{
	printf("Name: %s\n", who->name);
	printf("\tAge: %d\n",who->age);
	printf("\tHeight: %d\n", who->height);
	printf("\tWeight: %d\n", who->weight);
}
// In the main function I use all the previous functions and the struct Person to do the following.
int main(int argc, char *argv[])
{
	// make two people stuctures
	struct Person *joe = Person_create(
			"Joe Alex", 32, 64, 140);


	struct Person *frank = Person_create(
			"Frank Blank", 20, 72, 180);
	
	// print them out and where they are in memory 
	printf("Joe is at memory location %p:\n", joe);
	Person_print(joe);


	printf("Frank is at memory location %p:\n", frank);
	Person_print(frank);


	// make everyone age 20 years and print them again
	joe->age += 20;
	joe->height -= 2;
	joe->weight +=40;
	Person_print(joe);


	frank->age += 20;
	frank->weight += 20;
	Person_print(frank);


	// destroy them both so we clean up
	Person_destroy(joe);
	Person_destroy(frank);


	return 0;
}




//whats meant when zed says pass NULL to Person_destroy and Person_print