From 3a0266271324bb04a68dc43ec84ac5aa956f76c1 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Mon, 11 Nov 2024 15:49:12 -0500 Subject: [PATCH] Add pointer test, and reading files. Modify struct test, add flip bools test, and array test. --- .vscode/settings.json | 5 +++- CMakeLists.txt | 2 ++ src/main.c | 67 ++++++++++++++++++++++++++++++++++++++++++- src/pointer_test.c | 18 ++++++++++++ src/pointer_test.h | 1 + src/read_file.c | 26 +++++++++++++++++ src/read_file.h | 1 + src/struct_test.c | 10 +++++++ src/struct_test.h | 8 +++++- 9 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 src/pointer_test.c create mode 100644 src/pointer_test.h create mode 100644 src/read_file.c create mode 100644 src/read_file.h diff --git a/.vscode/settings.json b/.vscode/settings.json index b460d47..0663298 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "files.associations": { - "math_functions.h": "c" + "math_functions.h": "c", + "pointer_test.h": "c", + "struct_test.h": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c43893a..b1e2a89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ src/main.c src/hello.c src/math_functions.c src/struct_test.c +src/pointer_test.c +src/read_file.c ) # https://discourse.cmake.org/t/how-to-properly-include-header-files/4762 diff --git a/src/main.c b/src/main.c index 1a7c206..fc634d5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,9 @@ #include "math_functions.h" #include "hello.h" #include "struct_test.h" +#include "pointer_test.h" +#include "read_file.h" + // Test // https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c @@ -13,6 +16,8 @@ #include #endif +#include "stdbool.h" + // TODO Move this into a different project. #ifdef _WIN32 @@ -23,14 +28,59 @@ void guiTest(){ #endif +// #define _FLIP_BOOLS +// Hmm, very evil +#ifdef _FLIP_BOOLS +// This is just madness, inverting the boolean values :P +#define true FALSE +#define false TRUE +// #define true (rand() % 2) +// #define false (rand() % 2) +#endif //_FLIP_BOOLS + // TODO Look into making a gtk C program // https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/ +// Print out the memory addresses for each item in an array. +void array_test() { + // https://www.tutorialspoint.com/cprogramming/c_memory_address.htm + // Int array + int numbers[5] = {1,2,3,4,5}; + int i = 0; + + printf("numbers = %p\n", numbers); + + do { + printf("numbers[%u] = %p\n", i, (void *)&numbers[i]); + i++; + } while (i < 5); + + printf("sizeof(numbers) = %lu\n", sizeof(numbers)); + +} + +// https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm +void check_even_odd(int num) { + (num %2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num); +} + + int main() { char welcomeMsg[] = "Welcome to KCNet"; // printf(welcomeMsg); uint16_t test = -2; + + // array_test(); + // for (int i=0; i<5; i++) { + // check_even_odd(i); + // } + // check_even_odd(1); + + // new_struct(); + read_file("test.txt"); + + // This doesn't work // uint16_test(); @@ -42,7 +92,22 @@ int main() { // printf("Number: %" PRIu32, test); // mathFunctions(); - print_struct(); + // print_struct(); + int i = 22222; + int* i_ptr = &i; + // pointer_madness(i_ptr); + +#ifdef _FLIP_BOOLS + bool testb = false; + if (testb) { + printf("True"); + } else { + printf("False"); + } + +#endif //_FLIP_BOOLS + + // printf_example(); return 0; diff --git a/src/pointer_test.c b/src/pointer_test.c new file mode 100644 index 0000000..5c00daf --- /dev/null +++ b/src/pointer_test.c @@ -0,0 +1,18 @@ +#include +#include "pointer_test.h" + + +// https://www.freecodecamp.org/news/pointers-in-c-are-not-as-difficult-as-you-think/ +// https://www.tutorialspoint.com/cprogramming/c_pointers.htm + +// https://stackoverflow.com/questions/32914298/print-value-and-address-of-pointer-defined-in-function +void pointer_madness(int* test) { + // int *test = 0; + // test = 2; + + printf("Value pointed to: %i\n", *test); + + printf("Address pointed to by test: 0x%x\n", (unsigned int)test); + printf("Address of test itself: 0x%x\n", (unsigned int) &test); + +} \ No newline at end of file diff --git a/src/pointer_test.h b/src/pointer_test.h new file mode 100644 index 0000000..fed689e --- /dev/null +++ b/src/pointer_test.h @@ -0,0 +1 @@ +void pointer_madness(int* test); \ No newline at end of file diff --git a/src/read_file.c b/src/read_file.c new file mode 100644 index 0000000..5312dbc --- /dev/null +++ b/src/read_file.c @@ -0,0 +1,26 @@ +#include +#include + +// https://solarianprogrammer.com/2019/04/03/c-programming-read-file-lines-fgets-getline-implement-portable-getline/ + +// This works! The file has to be where the .exe is which makes sense. +void read_file(const char* file_name) { + FILE *fp = fopen(file_name, "r"); + + // Check if file exists + if (fp == NULL) { + // perror("Unable to open file %s!", file_name); + perror("Unable to open file! "); + exit(1); + } + + char chunk[128]; + + while (fgets(chunk, sizeof(chunk), fp) != NULL) { + fputs(chunk, stdout); + // fputs("\n", stdout); // Marker string used to show where the content of the chunk array has ended. + } + + // Close the file + fclose(fp); +} \ No newline at end of file diff --git a/src/read_file.h b/src/read_file.h new file mode 100644 index 0000000..da82803 --- /dev/null +++ b/src/read_file.h @@ -0,0 +1 @@ +void read_file(const char* file_name); \ No newline at end of file diff --git a/src/struct_test.c b/src/struct_test.c index a2d6dca..08c415e 100644 --- a/src/struct_test.c +++ b/src/struct_test.c @@ -7,6 +7,16 @@ // https://www.w3schools.com/c/c_structs.php +void new_struct() { + struct structT1_ s1; + + strcpy(structT1.username, "user"); + structT1.tag_number = 22; + // s1.username = "user"; + // s1.tag_number = 22; + + printf("Username: %s\nTag number: %i", structT1.username, structT1.tag_number); +} void print_struct(){ struct TestStruct s1; diff --git a/src/struct_test.h b/src/struct_test.h index 5328f5a..a3ffa2c 100644 --- a/src/struct_test.h +++ b/src/struct_test.h @@ -8,6 +8,12 @@ struct TestStruct { int age; }; +struct structT1_ { + char username[20]; + int tag_number; + +} structT1; + // Not sure how to use this #ifdef _TEST // https://stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct @@ -25,4 +31,4 @@ char *addr, char*bd, char sex); #endif //_TEST void print_struct(); - +void new_struct();