Lab materials
- Slides: Introduction
- Slides: Pointers
Reminder on style
- Your code should be easily readable
- Have a uniform style
- Simple over bloated
- Be commented (not required for lab tasks, tests and exams)
- Be modular (code is divided into short, simple, easily reusable functions that you can reuse in the future, including during the test and the exam)
- Use optimal algorithms and data structures (covered so far during the course)
- Typically avoid global variables (we will see exceptions this semester)
Tasks
There are two lab tasks this time. Second task is given in two separately graded parts. Advanced task builds on the second task.
Lab task 1: Pointer to a variable
This is a classical swap the two values in a function task.
Download the starter code here: https://blue.pri.ee/ttu/files/iax0584/aluskoodid/t1_swap_template.c
Requirements
- Use the starter code provided
- All parts you need to complete are marked with TODO in the starter code
- Create two functions according to function comments in the starter code. One to read values, second to swap values. Call those out
- Print out the memory addresses where the data is stored in 3 different locations
- In main function, the original locations of the variables
- In the reading function, show the addresses where you are storing the values
- In the swap function, show the location where the values are you are swapping
- Explain the significance of the address values when defending!
Testing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
risto@risto-lt3:~/Nextcloud/work/ttu/teaching/_generic/prog2/lab/pointers$ ./t1_swap Addresses in function main x @ 0x7ffffd82f7d0 y @ 0x7ffffd82f7d4 Addresses in function ReadValues x @ 0x7ffffd82f7d0 y @ 0x7ffffd82f7d4 Enter value x: 19 Enter value y: 4 Original values in main x = 19 y = 4 Addresses in function SwapValues x @ 0x7ffffd82f7d0 y @ 0x7ffffd82f7d4 Updated values in main x = 4 y = 19 |
Lab task 2 part 1: Pointer to an array
In the second lab task, we will explore the memory addresses in an integer array and use pointers to get multiple values out of a function. Additionally, we will be practicing pointer arithmetic.
Requirements
Create a program that fulfills the following requirements
- During this task, you are not permitted to use square brackets [] for indexing the array. For indexing, pointer arithmetic must be used. The array itself can still be declared using square brackets.
- Create a function, that reads 10 numbers and store them in an array.
Reminder: Pass both the array and the length of the array to functions!
Hint: read them from a file or use stream redirection to speed up testing! - Create a function that prints out all numbers and the memory address where they are stored
- Create a function that find both the minimum and maximum number
- You can only call this function once – it must find both results in one go and store them
- The function return type must be void .
- Assume that only one minimum and maximum exists
- Use the concept of pointers to solve getting multiple values from a function.
- Do not use arrays to store min/max values!
- Do not print the results in this function.
- All created functions must be called from the main() function.
- Minimum and maximum value are printed in the main() function.
Testing
Expected output of part 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
risto@risto-lt3:~/Nextcloud/work/ttu/teaching/_generic/prog2/lab/pointers$ ./t2_array Enter value for index 0 / 9: 19 Enter value for index 1 / 9: -1 Enter value for index 2 / 9: 125 Enter value for index 3 / 9: -43 Enter value for index 4 / 9: 15 Enter value for index 5 / 9: -12 Enter value for index 6 / 9: 175 Enter value for index 7 / 9: 54 Enter value for index 8 / 9: 8 Enter value for index 9 / 9: 119 Original array printout 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 0x7fff3ab96b70: 8 0x7fff3ab96b74: 119 Min: -43 Max: 175 |
Lab task 2 part 2: concept of pointers to arrays
The purpose of part 2 of this task is to make sure you understood the concept that an array can be interpreted as the pointer to the first member.
Requirements
- Create (or reuse) a function that is able to print n numbers and their memory addresses in the array.
- The function can only have two parameters.
- The function must be built in a way that it is able to print any arbitrary sequence of values from that array.
- To demonstrate that you wrote this function correctly (and understood the topic correctly), call the function 3 times, so that
- it will print out all members of the array
- it will print out elements 0 to 4
- it will print out elements 3 – 9
Hint: Think before acting! This part of the task is about the concept of pointers and arrays rather than creating some very clever and devious solution! Do not attempt to over-engineer the solution, it is not a puzzle!
Testing
Expected output of part 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
risto@risto-lt3:~/Nextcloud/work/ttu/teaching/_generic/prog2/lab/pointers$ ./t2_array Enter value for index 0 / 9: 19 Enter value for index 1 / 9: -1 Enter value for index 2 / 9: 125 Enter value for index 3 / 9: -43 Enter value for index 4 / 9: 15 Enter value for index 5 / 9: -12 Enter value for index 6 / 9: 175 Enter value for index 7 / 9: 54 Enter value for index 8 / 9: 8 Enter value for index 9 / 9: 119 Original array printout 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 0x7fff3ab96b70: 8 0x7fff3ab96b74: 119 Min: -43 Max: 175 Array print from 0 to 9 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 0x7fff3ab96b70: 8 0x7fff3ab96b74: 119 Array print from 0 to 4 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 Array print from 3 to 7 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 |
Advanced task: minmax task with addresses
In this task, we will recreate the minmax function from part 1 of task 2 . We add the ability to also print the memory addresses and indexes for the minimum and maximum value.
Requirements
- Create a new function or alter the existing one to find the locations of the minimum and maximum elements in the array in one go.
- The return type of this function must be void .
- The function can have 4 parameters
- Array
- Length of the array
- 2 pointers or double pointers – figure out what to store in them!
- In the main function, print out the following results
- The min and max value
- The addresses of where the min and max are in the original array
- The indexes of where the min and max value are in the original array.
- NB! You are not allowed to loop through the array more than once (e.g. once to find min/max, second time to find the position)! You must use the idea of pointers to calculate the results!
Testing
Expected output of Advanced task
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
risto@risto-lt3:~/Nextcloud/work/ttu/teaching/_generic/prog2/lab/pointers$ ./t2_array Enter value for index 0 / 9: 19 Enter value for index 1 / 9: -1 Enter value for index 2 / 9: 125 Enter value for index 3 / 9: -43 Enter value for index 4 / 9: 15 Enter value for index 5 / 9: -12 Enter value for index 6 / 9: 175 Enter value for index 7 / 9: 54 Enter value for index 8 / 9: 8 Enter value for index 9 / 9: 119 Original array printout 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 0x7fff3ab96b70: 8 0x7fff3ab96b74: 119 Min: -43 Max: 175 Array print from 0 to 9 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 0x7fff3ab96b70: 8 0x7fff3ab96b74: 119 Array print from 0 to 4 0x7fff3ab96b50: 19 0x7fff3ab96b54: -1 0x7fff3ab96b58: 125 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 Array print from 3 to 7 0x7fff3ab96b5c: -43 0x7fff3ab96b60: 15 0x7fff3ab96b64: -12 0x7fff3ab96b68: 175 0x7fff3ab96b6c: 54 Min -43; address 0x7fff3ab96b5c; index 3 Max 175; address 0x7fff3ab96b68; index 6 |
After the class, you should
- Understand the requirements for this subject, including how to get a grade
- Understand what a pointer is
- Understand the significance of memory addresses
- Understand virtual and physical addresses
- Know what affects the address length
- Know the extra memory requirements for explicit use of pointers
- Be able to declare pointers
- Know the coding style aspects of declaring and using pointers
- Know what’s the importance of pointer data types
- Know the significance and use for NULL pointers
- Know the two pointer operators and where to use them
- Have some understanding of possible use cases for pointers
- Know how to use pointer arithmetic
- Have a better understanding of the scanf arguments and how pointers come into play here
- Understand why we said that arrays were editable when passed to a function, but variables were not
- Be able to pass the addresses of variables to functions as pointers
- Be able to edit multiple variables in a function
- Understand the concept of double pointers
- Know how to use a ternary operator
Additional content
- C pointers
https://www.geeksforgeeks.org/c-pointers - Pointers – CS50 shorts
https://www.youtube.com/watch?v=XISnO2YhnsY