4 KiB
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
-
Create a file called
pointer_fun_again.c. -
Define a struct called
PlayStructwith the following fields:int_valueof type integerdouble_valueof type doublea_stringof type string (array ofcharof the length64)
-
Write a
mainfunction which declares- a struct
play_structof typePlayStruct - a pointer to a
PlayStructcalledplay_struct_pointer
- a struct
-
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?
-
Define a function
print_structwhich accepts the following paramters- a struct
PlayStructcalledps - a pointer to a
PlayStructcalledpps
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 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 toppsis passed to the function.
- a struct
-
Call the function
print_structin themainfunction and test your implementation. -
Define a function
change_structwhich accepts the same parameters asprint_struct. In the function body the values of the two parameters shall be changed to some different values. -
Call the function
change_structand then again callprint_structat the end of themainfunction. Which values are changed, which are not? Why? Describe this briefly in a comment right after the call of your function. -
Define a function
print_stringwhich accepts a string parameterstring_to_printand prints the content of the string into the terminal. -
Call the function
print_stringand pass it the fielda_stringof the variableplay_struct. Test your implementation. -
Define a function
change_stringwhich accepts two parameters, a stringstring1and a pointer to a stringp_string. The function shall change the thirdcharofstring1and the secondcharofp_stringto\0. -
Call the function
change_stringagain with the fielda_stringand the pointer to a newly created stringanother_stringof length16which is given some initial value. -
Finally call
print_stringagain with the two parameters you passed tochange_stringbefore. 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