From 33824b31f663b162eca2969f9c3002aba18e1022 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Tue, 16 Nov 2021 09:11:54 +0100 Subject: [PATCH] Added comments --- BlogDB.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 88afc08..e46dfa4 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -850,8 +850,8 @@ int PostIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - printf("%d\n", count); free(result); @@ -865,18 +865,28 @@ int PostIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -903,8 +913,8 @@ int ActivePostIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - printf("%d\n", count); free(result); @@ -918,18 +928,28 @@ int ActivePostIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -956,8 +976,8 @@ int AuthorIDs(int** arr, MYSQL* con) MYSQL_ROW row; row = mysql_fetch_row(result); + // Get the Count int count = atoi(row[0]); - //printf("%d\n", count); free(result); @@ -971,18 +991,28 @@ int AuthorIDs(int** arr, MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); + // Allocate the array that was passed by reference + // To be one larger than I need. *arr = (int *)malloc(sizeof(int) * (count + 1)); - //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + // loop count times. for (int i = 0; i < count; ++i) { + //Get row row = mysql_fetch_row(result2); + + // If the row exists (I counted them, so it must exist.) if (row != NULL) { + // Set the i-th element of the array, that was passed by reference, to be the current row's ID *(*arr + i) = atoi(row[0]); } } + // Set the last element in the array passed by reference to -1, + // As an ID can never be negative, it can ensure that it never + // Enters unknown memory. *(*arr + count) = -1; + // Return the count, for easy looping. return count; } @@ -1035,10 +1065,10 @@ int main(int argc, char *argv[]) // Generate array of IDs int *postIDs; - int count2 = PostIDs(&postIDs, NULL); + int count1 = PostIDs(&postIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; i < count2; ++i) + for (int i = 0; i < count1; ++i) { // Print contents on a new line printf("%d\n", postIDs[i]); @@ -1057,10 +1087,10 @@ int main(int argc, char *argv[]) // Generate array of IDs int *activePostIDs; - int count1 = AuthorIDs(&activePostIDs, NULL); + int count2 = ActivePostIDs(&activePostIDs, NULL); // Loop until element is negative i.e. the last - for (int i = 0; i < count1; ++i) + for (int i = 0; i < count2; ++i) { // Print contents on a new line printf("%d\n", activePostIDs[i]);