Note: If you haven’t done the pointers tutorial , it might help you to keep things straight!
Pointers and structures
Update your lab repository by cd
ing to your labs
directory and running git pull
. Then, cd
into the subdirectory pointers
.
- Examine the header file
complex.h
and its correspondingcomplex.cpp
. This file provides a structure and some functions for working with complex numbers. - Create a new file named
main.cpp
. Then, do the following:#include
the header filecomplex.h
- Write the usual
main
function and#include <iostream>
- Declare three variables of type
Complex
and initialize two of them to numbers of your choice - Call the
add
function, passing the uninitializedComplex
number as thesum
parameter, and print out the result usingdisplay
Note: I didn’t create the
main.cpp
file for you this time! Don’t forget to#include "complex.h"
.
Next, define a new function named
add
so that it takes a pointer to thesum
object instead of a reference. Add the function declaration tocomplex.h
as well. Then, update yourmain
function to call the new version ofadd
and print out the result. Think about:- What changes do you have to make to the function definition?
- What changes do you have to make to the function call?
- Is the pointer being passed by value or by reference? Should it be
const
? - Is one of the definitions (pointer vs reference) “better” than the other, and why? Can you think of another way to hand back the result of the function?
Tip: C++ supports function overloading , which allows you to define multiple functions with the same name and different parameters. This means you can write a new
add
function without deleting the old one!
Modify
complex.h
andcomplex.cpp
to add a function namedsort_3
that takes 3 pointers to complex numbers as input. The function must sort the numbers by their magnitude such that the first number is the smallest and the last number is the largest.You must sort by swapping the pointers themselves, not the values! This means that you’ll need to pass the pointers by reference.
A sample call to this function might be as follows:
Complex c1 = { 3, 5 }; Complex c2 = { 17, -2 }; Complex c3 = { 1, 1 }; Complex *p1 = &c1; Complex *p2 = &c2; Complex *p3 = &c3; sort_3(p1, p2, p3); cout << "The sorted numbers are: "; display(*p1); cout << ", "; display(*p2); cout << ", "; display(*p3); cout << endl;
which should print out:
The sorted numbers are: 1+1i, 3+5i, 17β2i
Tracing and debugging
The program segment below contains an alternative way of printing the complex number using pointers instead of pass-by-value, but it has at least one error which will cause (or is likely to cause) a runtime error. Trace it and try to find the error, then fix it.
// in main.cpp
Complex f1 = { 3, 5 }; // 3 + 5i
Complex f2 = { 17, -2 }; // 17 β 2i
Complex *sum;
add(f1, f2, sum);
cout << "The sum of ";
write_complex(&f1);
cout << " and ";
write_complex(&f2);
cout << " is ";
write_complex(sum);
cout << endl;
// in complex.cpp
void write_complex(const Complex *c) {
cout << c->real << ' ';
if (c->imag >= 0)
cout << '+';
else
cout << '-';
cout << ' ' << abs(c->imag) << 'i';
}
Extra practice with const
Given the following declarations:
int i = 10;
int j = 20;
int *p1 = &i;
const int *p2 = &i;
int *const p3 = &i;
const int *const p4 = &i;
Which of the following operations are legal? If legal, what is the result? You may find it useful to draw the memory diagram for each operation.
Hint: remember that declarations are read right-to-left, so
const int *p
means “pointer toconst int
”, whileint *const p
means “constant pointer toint
”.
# | Expression | Result? |
---|---|---|
1 | *p1 = 30; | |
2 | *p2 = 40; | |
3 | *p3 = 50; | |
4 | *p4 = 60; | |
5 | p1 = &j; | |
6 | p2 = &j; | |
7 | p3 = &j; | |
8 | p4 = &j; | |
9 | i = 0; | |
10 | j = 0; |