Add pointer test, and reading files.
Modify struct test, add flip bools test, and array test.
This commit is contained in:
parent
6d7cb3760b
commit
3a02662713
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -1,5 +1,8 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"math_functions.h": "c"
|
"math_functions.h": "c",
|
||||||
|
"pointer_test.h": "c",
|
||||||
|
"struct_test.h": "c",
|
||||||
|
"stdio.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,6 +14,8 @@ src/main.c
|
|||||||
src/hello.c
|
src/hello.c
|
||||||
src/math_functions.c
|
src/math_functions.c
|
||||||
src/struct_test.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
|
# https://discourse.cmake.org/t/how-to-properly-include-header-files/4762
|
||||||
|
67
src/main.c
67
src/main.c
@ -6,6 +6,9 @@
|
|||||||
#include "math_functions.h"
|
#include "math_functions.h"
|
||||||
#include "hello.h"
|
#include "hello.h"
|
||||||
#include "struct_test.h"
|
#include "struct_test.h"
|
||||||
|
#include "pointer_test.h"
|
||||||
|
#include "read_file.h"
|
||||||
|
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
// https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c
|
// https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c
|
||||||
@ -13,6 +16,8 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "stdbool.h"
|
||||||
|
|
||||||
|
|
||||||
// TODO Move this into a different project.
|
// TODO Move this into a different project.
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -23,14 +28,59 @@ void guiTest(){
|
|||||||
|
|
||||||
#endif
|
#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
|
// TODO Look into making a gtk C program
|
||||||
// https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/
|
// 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() {
|
int main() {
|
||||||
char welcomeMsg[] = "Welcome to KCNet";
|
char welcomeMsg[] = "Welcome to KCNet";
|
||||||
// printf(welcomeMsg);
|
// printf(welcomeMsg);
|
||||||
|
|
||||||
uint16_t test = -2;
|
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
|
// This doesn't work
|
||||||
// uint16_test();
|
// uint16_test();
|
||||||
|
|
||||||
@ -42,7 +92,22 @@ int main() {
|
|||||||
// printf("Number: %" PRIu32, test);
|
// printf("Number: %" PRIu32, test);
|
||||||
|
|
||||||
// mathFunctions();
|
// 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();
|
// printf_example();
|
||||||
return 0;
|
return 0;
|
||||||
|
18
src/pointer_test.c
Normal file
18
src/pointer_test.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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);
|
||||||
|
|
||||||
|
}
|
1
src/pointer_test.h
Normal file
1
src/pointer_test.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
void pointer_madness(int* test);
|
26
src/read_file.c
Normal file
26
src/read_file.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
1
src/read_file.h
Normal file
1
src/read_file.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
void read_file(const char* file_name);
|
@ -7,6 +7,16 @@
|
|||||||
|
|
||||||
// https://www.w3schools.com/c/c_structs.php
|
// 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(){
|
void print_struct(){
|
||||||
struct TestStruct s1;
|
struct TestStruct s1;
|
||||||
|
@ -8,6 +8,12 @@ struct TestStruct {
|
|||||||
int age;
|
int age;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct structT1_ {
|
||||||
|
char username[20];
|
||||||
|
int tag_number;
|
||||||
|
|
||||||
|
} structT1;
|
||||||
|
|
||||||
// Not sure how to use this
|
// Not sure how to use this
|
||||||
#ifdef _TEST
|
#ifdef _TEST
|
||||||
// https://stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct
|
// 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
|
#endif //_TEST
|
||||||
|
|
||||||
void print_struct();
|
void print_struct();
|
||||||
|
void new_struct();
|
||||||
|
Loading…
Reference in New Issue
Block a user