Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 2.4
Pointers in combination with references


In this exercise we will explore pointers and references relative to each other. Play and learn! We do this in context of a function that returns the maximum of two doubles. Here is the function with call-by-value parameters:

  double max(double a, double b){
    return a < b ? b : a;
  }

In all cases below, program the max function, call it, make make sure you get the expected result from the call.

  1. As a warm up: Make a version with call by const references instead of call by value parameters.
  2. Demonstrate that pointers to references do not exist.
  3. Make a version with references to pointers. Does that make sense to you?
  4. Next, make version with const references to pointers.
  5. Finally, a version with const references to pointers to double constants

Can you imagine other interesting variations?


Solution