Rewrote the file, works. Old code renamed to .old & changed .gitignore to ignore the .vscode folder
This commit is contained in:
parent
4d2785339c
commit
f1af8badb1
5 changed files with 221 additions and 164 deletions
107
.gitignore
vendored
107
.gitignore
vendored
|
|
@ -1,52 +1,55 @@
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
|
||||||
# Object files
|
# Object files
|
||||||
*.o
|
*.o
|
||||||
*.ko
|
*.ko
|
||||||
*.obj
|
*.obj
|
||||||
*.elf
|
*.elf
|
||||||
|
|
||||||
# Linker output
|
# Linker output
|
||||||
*.ilk
|
*.ilk
|
||||||
*.map
|
*.map
|
||||||
*.exp
|
*.exp
|
||||||
|
|
||||||
# Precompiled Headers
|
# Precompiled Headers
|
||||||
*.gch
|
*.gch
|
||||||
*.pch
|
*.pch
|
||||||
|
|
||||||
# Libraries
|
# Libraries
|
||||||
*.lib
|
*.lib
|
||||||
*.a
|
*.a
|
||||||
*.la
|
*.la
|
||||||
*.lo
|
*.lo
|
||||||
|
|
||||||
# Shared objects (inc. Windows DLLs)
|
# Shared objects (inc. Windows DLLs)
|
||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.so.*
|
*.so.*
|
||||||
*.dylib
|
*.dylib
|
||||||
|
|
||||||
# Executables
|
# Executables
|
||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
*.i*86
|
*.i*86
|
||||||
*.x86_64
|
*.x86_64
|
||||||
*.hex
|
*.hex
|
||||||
|
|
||||||
# Debug files
|
# Debug files
|
||||||
*.dSYM/
|
*.dSYM/
|
||||||
*.su
|
*.su
|
||||||
*.idb
|
*.idb
|
||||||
*.pdb
|
*.pdb
|
||||||
|
|
||||||
# Kernel Module Compile Results
|
# Kernel Module Compile Results
|
||||||
*.mod*
|
*.mod*
|
||||||
*.cmd
|
*.cmd
|
||||||
.tmp_versions/
|
.tmp_versions/
|
||||||
modules.order
|
modules.order
|
||||||
Module.symvers
|
Module.symvers
|
||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode/
|
||||||
136
README.md
136
README.md
|
|
@ -1,68 +1,68 @@
|
||||||
[](https://classroom.github.com/a/p27yoBeU)
|
[](https://classroom.github.com/a/p27yoBeU)
|
||||||
## IF.03.22 Procedural Programming
|
## IF.03.22 Procedural Programming
|
||||||
# Assignment More Pointer Fun
|
# Assignment More Pointer Fun
|
||||||
|
|
||||||
## Objective
|
## Objective
|
||||||
This assignment lets you practise the usage of structs, arrays and pointers to structs.
|
This assignment lets you practise the usage of structs, arrays and pointers to structs.
|
||||||
|
|
||||||
## Materials
|
## Materials
|
||||||
- VS Code
|
- VS Code
|
||||||
- gcc
|
- gcc
|
||||||
- terminal.
|
- terminal.
|
||||||
|
|
||||||
## Required Tasks
|
## Required Tasks
|
||||||
1. Create a file called `pointer_fun_again.c`.
|
1. Create a file called `pointer_fun_again.c`.
|
||||||
1. Define a struct called `PlayStruct` with the following fields:
|
1. Define a struct called `PlayStruct` with the following fields:
|
||||||
- `int_value` of type integer
|
- `int_value` of type integer
|
||||||
- `double_value` of type double
|
- `double_value` of type double
|
||||||
- `a_string` of type string (array of `char` of the length `64`)
|
- `a_string` of type string (array of `char` of the length `64`)
|
||||||
1. Write a `main` function which declares
|
1. Write a `main` function which declares
|
||||||
- a struct `play_struct` of type `PlayStruct`
|
- a struct `play_struct` of type `PlayStruct`
|
||||||
- a pointer to a `PlayStruct` called `play_struct_pointer`
|
- 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?
|
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
|
2. Define a function `print_struct` which accepts the following paramters
|
||||||
- a struct `PlayStruct` called `ps`
|
- a struct `PlayStruct` called `ps`
|
||||||
- a pointer to a `PlayStruct` called `pps`
|
- a pointer to a `PlayStruct` called `pps`
|
||||||
|
|
||||||
The function shall print both parameters in the following form:
|
The function shall print both parameters in the following form:
|
||||||
- `Values of struct ps: <x>, <y>, <z>` where `<x>`, `<y>` and `<z>` shall be replaced by the actual values of the struct's fields passed to the function.
|
- `Values of struct ps: <x>, <y>, <z>` where `<x>`, `<y>` and `<z>` shall be replaced by the actual values of the struct's fields passed to the function.
|
||||||
- `Values of struct pps: <x>, <y>, <z>` where `<x>`, `<y>` and `<z>` 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.
|
- `Values of struct pps: <x>, <y>, <z>` where `<x>`, `<y>` and `<z>` 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.
|
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.
|
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.
|
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. 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. 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. 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. 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.
|
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
|
## Hints
|
||||||
- Take care to keep the work loop "Implement a little", "Test a little" to avoid the 100 lines of error mess.
|
- Take care to keep the work loop "Implement a little", "Test a little" to avoid the 100 lines of error mess.
|
||||||
|
|
||||||
## Extra Credit
|
## Extra Credit
|
||||||
Document your implementation in an extra text file or in an inline comment. In particular:
|
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.
|
- 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.
|
- Give alternatives how to pass parameters when calling the different functions.
|
||||||
- Write down the reasons, why some variables are not changed by `change_integers`.
|
- Write down the reasons, why some variables are not changed by `change_integers`.
|
||||||
|
|
||||||
## Evaluation
|
## Evaluation
|
||||||
All coding assignments will get checked. Most common reasons that your assignment is marked down are:
|
All coding assignments will get checked. Most common reasons that your assignment is marked down are:
|
||||||
|
|
||||||
- Program does not build or builds with warnings
|
- Program does not build or builds with warnings
|
||||||
- One or more items in the *Required Tasks* section are not satisfied
|
- One or more items in the *Required Tasks* section are not satisfied
|
||||||
- Submitted code is visually sloppy and hard to read
|
- Submitted code is visually sloppy and hard to read
|
||||||
|
|
||||||
## Things to Learn
|
## Things to Learn
|
||||||
- Repeat using complex data types, like structs, arrays
|
- Repeat using complex data types, like structs, arrays
|
||||||
- Repeat implementing functions
|
- Repeat implementing functions
|
||||||
- Repeat pointer handling on complex data types
|
- Repeat pointer handling on complex data types
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,45 +1,54 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <string.h>
|
|
||||||
|
struct PlayStruct {
|
||||||
struct PlayStruct {
|
int int_value;
|
||||||
int int_value;
|
double double_value;
|
||||||
double double_value;
|
char a_string[64];
|
||||||
char* a_string;
|
};
|
||||||
};
|
|
||||||
|
void print_struct(struct PlayStruct ps, struct PlayStruct* pps)
|
||||||
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 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);
|
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) {
|
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;
|
ps.int_value = 2;
|
||||||
}
|
ps.double_value = 3.0;
|
||||||
|
|
||||||
void print_string(char* string_to_print) {
|
pps->int_value = 4;
|
||||||
printf("%s\n", string_to_print);
|
pps->double_value = 5.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void change_string(char* string1, char** p_string) {
|
void print_string(char string_to_print[])
|
||||||
string1[2] = '\0';
|
{
|
||||||
*p_string[1] = '\0';
|
printf("%s\n", string_to_print);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
void change_string(char string1[], char *p_string)
|
||||||
struct PlayStruct play_struct = {1, 2.0, "3456"};
|
{
|
||||||
struct PlayStruct* play_struct_pointer = &play_struct;
|
string1[2] = '\0';
|
||||||
|
p_string[1] = '\0';
|
||||||
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
|
int main(int argc, char* argv[])
|
||||||
print_struct(play_struct, play_struct_pointer);
|
{
|
||||||
|
struct PlayStruct play_struct = {1, 2.0, "Hello!"};
|
||||||
print_string(play_struct.a_string);
|
|
||||||
|
print_struct(play_struct, &play_struct);
|
||||||
char* string2 = "Test";
|
change_struct(play_struct, &play_struct);
|
||||||
change_string(play_struct.a_string, &string2);
|
//only the struct from the pointer will be changed, the other one doesnt, as it is a local var
|
||||||
print_string(string2);
|
print_struct(play_struct, &play_struct);
|
||||||
return 0;
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
45
pointer_fun_again.c.old
Normal file
45
pointer_fun_again.c.old
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue