PR2EN1: Pointers

Lab materials

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

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

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

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

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