Added comments

This commit is contained in:
Adam Fordsmand 2021-11-16 09:11:54 +01:00
parent 9534d412f8
commit 33824b31f6
1 changed files with 40 additions and 10 deletions

View File

@ -850,8 +850,8 @@ int PostIDs(int** arr, MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
printf("%d\n", count);
free(result); free(result);
@ -865,18 +865,28 @@ int PostIDs(int** arr, MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(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)); *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) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { 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]); *(*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; *(*arr + count) = -1;
// Return the count, for easy looping.
return count; return count;
} }
@ -903,8 +913,8 @@ int ActivePostIDs(int** arr, MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
printf("%d\n", count);
free(result); free(result);
@ -918,18 +928,28 @@ int ActivePostIDs(int** arr, MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(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)); *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) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { 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]); *(*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; *(*arr + count) = -1;
// Return the count, for easy looping.
return count; return count;
} }
@ -956,8 +976,8 @@ int AuthorIDs(int** arr, MYSQL* con)
MYSQL_ROW row; MYSQL_ROW row;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
// Get the Count
int count = atoi(row[0]); int count = atoi(row[0]);
//printf("%d\n", count);
free(result); free(result);
@ -971,18 +991,28 @@ int AuthorIDs(int** arr, MYSQL* con)
MYSQL_RES *result2 = mysql_store_result(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)); *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) { for (int i = 0; i < count; ++i) {
//Get row
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
// If the row exists (I counted them, so it must exist.)
if (row != NULL) { 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]); *(*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; *(*arr + count) = -1;
// Return the count, for easy looping.
return count; return count;
} }
@ -1035,10 +1065,10 @@ int main(int argc, char *argv[])
// Generate array of IDs // Generate array of IDs
int *postIDs; int *postIDs;
int count2 = PostIDs(&postIDs, NULL); int count1 = PostIDs(&postIDs, NULL);
// Loop until element is negative i.e. the last // 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 // Print contents on a new line
printf("%d\n", postIDs[i]); printf("%d\n", postIDs[i]);
@ -1057,10 +1087,10 @@ int main(int argc, char *argv[])
// Generate array of IDs // Generate array of IDs
int *activePostIDs; int *activePostIDs;
int count1 = AuthorIDs(&activePostIDs, NULL); int count2 = ActivePostIDs(&activePostIDs, NULL);
// Loop until element is negative i.e. the last // 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 // Print contents on a new line
printf("%d\n", activePostIDs[i]); printf("%d\n", activePostIDs[i]);