Lab 14: Pointers lab

Feb 29, 2024  β”‚  m. Feb 29, 2024 by Charlotte Curtis

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 cding to your labs directory and running git pull. Then, cd into the subdirectory pointers.

  1. Examine the header file complex.h and its corresponding complex.cpp. This file provides a structure and some functions for working with complex numbers.
  2. Create a new file named main.cpp. Then, do the following:
    1. #include the header file complex.h
    2. Write the usual main function and #include <iostream>
    3. Declare three variables of type Complex and initialize two of them to numbers of your choice
    4. Call the add function, passing the uninitialized Complex number as the sum parameter, and print out the result using display

    Note: I didn’t create the main.cpp file for you this time! Don’t forget to #include "complex.h".

  1. Next, define a new function named add so that it takes a pointer to the sum object instead of a reference. Add the function declaration to complex.h as well. Then, update your main function to call the new version of add and print out the result. Think about:

    1. What changes do you have to make to the function definition?
    2. What changes do you have to make to the function call?
    3. Is the pointer being passed by value or by reference? Should it be const?
    4. 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!

  1. Modify complex.h and complex.cpp to add a function named sort_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 to const int”, while int *const p means “constant pointer to int”.

#ExpressionResult?
1*p1 = 30;
2*p2 = 40;
3*p3 = 50;
4*p4 = 60;
5p1 = &j;
6p2 = &j;
7p3 = &j;
8p4 = &j;
9i = 0;
10j = 0;


Previous: Lab 13: Pointers tutorial
Next: Lab 15: Dynamic Allocation and Valgrind