From f1af8badb1fb4f39c47fdc76e9a74373b16b9688 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 15 Oct 2024 19:19:33 +0200 Subject: [PATCH] Rewrote the file, works. Old code renamed to .old & changed .gitignore to ignore the .vscode folder --- .gitignore | 107 ++++++++++++++++--------------- README.md | 136 ++++++++++++++++++++-------------------- pointer_fun_again | Bin 18200 -> 16168 bytes pointer_fun_again.c | 97 +++++++++++++++------------- pointer_fun_again.c.old | 45 +++++++++++++ 5 files changed, 221 insertions(+), 164 deletions(-) create mode 100644 pointer_fun_again.c.old diff --git a/.gitignore b/.gitignore index c6127b3..56dda50 100644 --- a/.gitignore +++ b/.gitignore @@ -1,52 +1,55 @@ -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# VS Code +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 78c4a45..61c6a56 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,68 @@ -[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/p27yoBeU) -## IF.03.22 Procedural Programming -# Assignment More Pointer Fun - -## Objective -This assignment lets you practise the usage of structs, arrays and pointers to structs. - -## Materials -- VS Code -- gcc -- terminal. - -## Required Tasks -1. Create a file called `pointer_fun_again.c`. -1. Define a struct called `PlayStruct` with the following fields: - - `int_value` of type integer - - `double_value` of type double - - `a_string` of type string (array of `char` of the length `64`) -1. Write a `main` function which declares - - a struct `play_struct` of type `PlayStruct` - - a pointer to a `PlayStruct` called `play_struct_pointer` - -3. Assign some values to the variables (you may do this already while declaring the variables). Take care how to assign a value to the pointer. What is useful/possible there? - -2. Define a function `print_struct` which accepts the following paramters - - a struct `PlayStruct` called `ps` - - a pointer to a `PlayStruct` called `pps` - - The function shall print both parameters in the following form: - - `Values of struct ps: , , ` where ``, `` and `` shall be replaced by the actual values of the struct's fields passed to the function. - - `Values of struct pps: , , ` where ``, `` and `` shall be replaced by the actual values of the struct's fields passed to the function. Take care that here also the values are required to be printed although the address to `pps` is passed to the function. - -4. Call the function `print_struct` in the `main` function and test your implementation. - -5. Define a function `change_struct` which accepts the same parameters as `print_struct`. In the function body the values of the two parameters shall be changed to some different values. - -6. Call the function `change_struct` and then again call `print_struct` at the end of the `main` function. Which values are changed, which are not? Why? Describe this briefly in a comment right after the call of your function. - -7. Define a function `print_string` which accepts a string parameter `string_to_print` and prints the content of the string into the terminal. - -7. Call the function `print_string` and pass it the field `a_string` of the variable `play_struct`. Test your implementation. - -7. Define a function `change_string` which accepts two parameters, a string `string1` and a pointer to a string `p_string`. The function shall change the third `char` of `string1` and the second `char` of `p_string` to `\0`. - -7. Call the function `change_string` again with the field `a_string` and the pointer to a newly created string `another_string` of length `16` which is given some initial value. - -7. Finally call `print_string` again with the two parameters you passed to `change_string` before. Check the results and explain, what you observe and why. Again describe your observations in a comment after the call of the functions. - -## Hints -- Take care to keep the work loop "Implement a little", "Test a little" to avoid the 100 lines of error mess. - -## Extra Credit -Document your implementation in an extra text file or in an inline comment. In particular: -- Write down the reasons, how you assigned values to the variables in the main function. -- Give alternatives how to pass parameters when calling the different functions. -- Write down the reasons, why some variables are not changed by `change_integers`. - -## Evaluation -All coding assignments will get checked. Most common reasons that your assignment is marked down are: - -- Program does not build or builds with warnings -- One or more items in the *Required Tasks* section are not satisfied -- Submitted code is visually sloppy and hard to read - -## Things to Learn -- Repeat using complex data types, like structs, arrays -- Repeat implementing functions -- Repeat pointer handling on complex data types +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/p27yoBeU) +## IF.03.22 Procedural Programming +# Assignment More Pointer Fun + +## Objective +This assignment lets you practise the usage of structs, arrays and pointers to structs. + +## Materials +- VS Code +- gcc +- terminal. + +## Required Tasks +1. Create a file called `pointer_fun_again.c`. +1. Define a struct called `PlayStruct` with the following fields: + - `int_value` of type integer + - `double_value` of type double + - `a_string` of type string (array of `char` of the length `64`) +1. Write a `main` function which declares + - a struct `play_struct` of type `PlayStruct` + - a pointer to a `PlayStruct` called `play_struct_pointer` + +3. Assign some values to the variables (you may do this already while declaring the variables). Take care how to assign a value to the pointer. What is useful/possible there? + +2. Define a function `print_struct` which accepts the following paramters + - a struct `PlayStruct` called `ps` + - a pointer to a `PlayStruct` called `pps` + + The function shall print both parameters in the following form: + - `Values of struct ps: , , ` where ``, `` and `` shall be replaced by the actual values of the struct's fields passed to the function. + - `Values of struct pps: , , ` where ``, `` and `` shall be replaced by the actual values of the struct's fields passed to the function. Take care that here also the values are required to be printed although the address to `pps` is passed to the function. + +4. Call the function `print_struct` in the `main` function and test your implementation. + +5. Define a function `change_struct` which accepts the same parameters as `print_struct`. In the function body the values of the two parameters shall be changed to some different values. + +6. Call the function `change_struct` and then again call `print_struct` at the end of the `main` function. Which values are changed, which are not? Why? Describe this briefly in a comment right after the call of your function. + +7. Define a function `print_string` which accepts a string parameter `string_to_print` and prints the content of the string into the terminal. + +7. Call the function `print_string` and pass it the field `a_string` of the variable `play_struct`. Test your implementation. + +7. Define a function `change_string` which accepts two parameters, a string `string1` and a pointer to a string `p_string`. The function shall change the third `char` of `string1` and the second `char` of `p_string` to `\0`. + +7. Call the function `change_string` again with the field `a_string` and the pointer to a newly created string `another_string` of length `16` which is given some initial value. + +7. Finally call `print_string` again with the two parameters you passed to `change_string` before. Check the results and explain, what you observe and why. Again describe your observations in a comment after the call of the functions. + +## Hints +- Take care to keep the work loop "Implement a little", "Test a little" to avoid the 100 lines of error mess. + +## Extra Credit +Document your implementation in an extra text file or in an inline comment. In particular: +- Write down the reasons, how you assigned values to the variables in the main function. +- Give alternatives how to pass parameters when calling the different functions. +- Write down the reasons, why some variables are not changed by `change_integers`. + +## Evaluation +All coding assignments will get checked. Most common reasons that your assignment is marked down are: + +- Program does not build or builds with warnings +- One or more items in the *Required Tasks* section are not satisfied +- Submitted code is visually sloppy and hard to read + +## Things to Learn +- Repeat using complex data types, like structs, arrays +- Repeat implementing functions +- Repeat pointer handling on complex data types diff --git a/pointer_fun_again b/pointer_fun_again index 4a8898ac65a0b8c2b3f548d8ed2fe786bbb6a116..f6766462420af88f43c1fedd7bda1561bf7d37f0 100644 GIT binary patch delta 1199 zcmchXQAkr!7{~9qTeVHCI~_PA^$uJbnOmW54${rK>nI3BETuqSk{N>2r$yB5JG*oF(pz-k-2eHV?|;5?zjN+3 z^#IM>iPV(#*_eEDGIpkjaWa)m#Y9U|*PvD+4Qf)1>a=O3U|MQJ7Nj5G70fFU*>%H< z&nKeo6CcZmEzjGY=*D$-zukT&ji`PUqSs`ITi13NLjs4x9-j2*c@!oAeSr90i(j!gn!(~=ni_F1&Ms!x zq+y6)81ZIJT{yTsB#zz(<5)tNrnS(dd~UxP9K+XqzJ3$K%+tECC`7U7iQ$A0&vHUo z%IPvyKLl2D6B{{AWi{-XhOIiWxPe0*(>Uav!J-C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OJ6QsBBED2O-Cp_NdPEt|Wkr zRz(Kn`*8?T{sTILr`C%*O6m9R1{82Y1Ue`!=1&+Q_yV{xZQ(rpy$Iug?*X=Vm^eA> zo$@Zq*lXMtKcifypWPH?*G;p~0BagxJp*jd7;DskkYOgL3$>}5xo)kDBt90vs!(z$$5#HV{m&Zs1j zmerIJFBJ2oM3Jb<;W*rp+q3fhcNm^*PEF}-895m>MJ8b^P8h<9Tg`%ul3K{5R3#~b zyPPL-K9wLwa3>m5WD@dQp+-F`Az0YtnyD;9uVf7`nJXR4DrV{~Q^t$AxLzXJ`-@R= zPr0*K?CcJ8g*wFGp<+o@22+Zvfwx<3>IaWqi}V}@yQ%ispN?LmJ|XmQwtwbk{LOil#6nBvYemHhm|8rK9MOX zg>XlA@K7$V1kDt|iIPgz@c$=0Y=*`mG%ly$3_}SLE*0|OjGD-nlDHqqOfHnh+eV1? zH*SOGwb^P1I@&0`s2%IvOVir9&ZlWile@OUuZZ?o*Ddsl_I}qtU8`SI?b)4C=gJ4K zc4_A^{R_kPZEA8^{f z{*WsI?-?7E^KkzySYdzIx%z(|9q9F)6m)k&=`>@BLJ)$Q(5N-2~?=n2|<|pGitr`t;kv{FsXw;P+Fu>pFh5ZL{iMRv+ diff --git a/pointer_fun_again.c b/pointer_fun_again.c index 8796a1e..83c816d 100644 --- a/pointer_fun_again.c +++ b/pointer_fun_again.c @@ -1,45 +1,54 @@ -#include -#include -#include - -struct PlayStruct { - int int_value; - double double_value; - char* a_string; -}; - -void print_struct(struct PlayStruct ps, struct PlayStruct* pps) { - printf("Values of struct ps: %d, %lf, %s\n", ps.int_value, ps.double_value, ps.a_string); - printf("Values of struct pps: %d, %lf, %s\n", pps->int_value, pps->double_value, pps->a_string); -} - -void change_struct(struct PlayStruct ps, struct PlayStruct* pps) { - struct PlayStruct new_ps = {ps.int_value + 1, ps.double_value * 2, ps.a_string}; - *pps = new_ps; -} - -void print_string(char* string_to_print) { - printf("%s\n", string_to_print); -} - -void change_string(char* string1, char** p_string) { - string1[2] = '\0'; - *p_string[1] = '\0'; -} - -int main(int argc, char* argv[]) { - struct PlayStruct play_struct = {1, 2.0, "3456"}; - struct PlayStruct* play_struct_pointer = &play_struct; - - print_struct(play_struct, play_struct_pointer); - change_struct(play_struct, play_struct_pointer); - //first value gets incremented by 1 & second value gets multiplied by 2 & the string wont be changed - print_struct(play_struct, play_struct_pointer); - - print_string(play_struct.a_string); - - char* string2 = "Test"; - change_string(play_struct.a_string, &string2); - print_string(string2); - return 0; +#include +#include + +struct PlayStruct { + int int_value; + double double_value; + char a_string[64]; +}; + +void print_struct(struct PlayStruct ps, struct PlayStruct* pps) +{ + printf("Values of struct ps: %d, %lf, %s\n", ps.int_value, ps.double_value, ps.a_string); + printf("Values of struct pps %d, %lf, %s\n", pps->int_value, pps->double_value, pps->a_string); +} + +void change_struct(struct PlayStruct ps, struct PlayStruct* pps) +{ + ps.int_value = 2; + ps.double_value = 3.0; + + pps->int_value = 4; + pps->double_value = 5.0; +} + +void print_string(char string_to_print[]) +{ + printf("%s\n", string_to_print); +} + +void change_string(char string1[], char *p_string) +{ + string1[2] = '\0'; + p_string[1] = '\0'; +} + +int main(int argc, char* argv[]) +{ + struct PlayStruct play_struct = {1, 2.0, "Hello!"}; + + print_struct(play_struct, &play_struct); + change_struct(play_struct, &play_struct); + //only the struct from the pointer will be changed, the other one doesnt, as it is a local var + print_struct(play_struct, &play_struct); + + print_string(play_struct.a_string); + char another_string[16] = "World!"; + + change_string(play_struct.a_string, another_string); + print_string(play_struct.a_string); + print_string(another_string); + //both will be changed, because they are both pointers + + return 0; } \ No newline at end of file diff --git a/pointer_fun_again.c.old b/pointer_fun_again.c.old new file mode 100644 index 0000000..ee09389 --- /dev/null +++ b/pointer_fun_again.c.old @@ -0,0 +1,45 @@ +#include +#include +#include + +struct PlayStruct { + int int_value; + double double_value; + char* a_string; +}; + +void print_struct(struct PlayStruct ps, struct PlayStruct* pps) { + printf("Values of struct ps: %d, %lf, %s\n", ps.int_value, ps.double_value, ps.a_string); + printf("Values of struct pps: %d, %lf, %s\n", pps->int_value, pps->double_value, pps->a_string); +} + +void change_struct(struct PlayStruct ps, struct PlayStruct* pps) { + struct PlayStruct new_ps = {ps.int_value + 1, ps.double_value * 2, ps.a_string}; + *pps = new_ps; +} + +void print_string(char* string_to_print) { + printf("%s\n", string_to_print); +} + +void change_string(char* string1, char** p_string) { + string1[2] = '\0'; + *p_string[1] = '\0'; +} + +int main(int argc, char* argv[]) { + struct PlayStruct play_struct = {1, 2.0, "3456"}; + struct PlayStruct* play_struct_pointer = &play_struct; + + print_struct(play_struct, play_struct_pointer); + change_struct(play_struct, play_struct_pointer); + //first value gets incremented by 1 & second value gets multiplied by 2 & the string wont be changed + print_struct(play_struct, play_struct_pointer); + + print_string(play_struct.a_string); + + char* string2 = "Test"; + change_string(play_struct.a_string, &string2); + print_string(string2); + return 0; +} \ No newline at end of file