Chapter 8 - Worked Exercises
Exercise 1 - Pointers
Predict the output for each of the below examples.
The exact values such as memory addresses, or garbage data are not predictable. In such instances you can assume any value you of your choice, e.g. 'a memory address 1000'.
Also state if any errors will occur, e.g. the program will not compile, or a segmentation fault error will occur.
Question 1
int a = 0;
int* b = &a;
std::cout << b << std::endl;
std::cout << *b << std::endl;Question 2
int* b = new int;
int* c = new int(5);
std::cout << "b is " << *b << std::endl;
std::cout << "c is " << *c << std::endl;Question 3
int a = 10;
int* b = &a;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << *b << std::endl;
*b = 10;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << *b << std::endl;
std::cout << "address of a is " << &a << std::endl;
std::cout << "b stores address " << b << std::endl;Question 4
int* a;
std::cout << "a is " << *a << std::endl;Question 5
int a = 5;
std::cout << &a << std::endl;
std::cout << *&a << std::endl;
std::cout << &*&a << std::endl;
std::cout << *&*&a << std::endl;Question 6
int* a;
int* b;
int c = 4, d = 7;
a = &c;
b = a;
std::cout << "a is " << a << " which stores value " << *a << std::endl;
std::cout << "b is " << b << " which stores value " << *b << std::endl;
b = &d;
std::cout << "a is " << a << " which stores value " << *a << std::endl;
std::cout << "b is " << b << " which stores value " << *b << std::endl;
*a = *b;
std::cout << "a is " << a << " which stores value " << *a << std::endl;
std::cout << "b is " << b << " which stores value " << *b << std::endl;Exercise 2 - References
Predict the output for the below examples.
Question 1
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10, b = 20;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;
swap(a, b);
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;
}Does the above work as the programmer likely intended it to? If not, how can we fix this?
Question 2
(take your time with this one!)
void two(int c, char& d) {
std::cout << "(two) entering function two" << std::endl;
c *= 10;
d = 'Z';
std::cout << "c is " << c << std::endl;
std::cout << "d is " << d << std::endl;
std::cout << "(two) leaving function two" << std::endl;
}
void one(char x, int& y) {
std::cout << "(one) entering function one" << std::endl;
x = 'a';
y += 3;
std::cout << "x is " << x << std::endl;
std::cout << "y is " << y << std::endl;
two(y, x);
std::cout << "x is " << x << std::endl;
std::cout << "y is " << y << std::endl;
std::cout << "(one) leaving function one" << std::endl;
}
int main() {
char a = 'Q';
int b = 0;
std::cout << "(main) entering main" << std::endl;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;
one(a, b);
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;
std::cout << "(main) leaving main" << std::endl;
}Exercise 3 - Pointers and Arrays
Strings as character pointers
As we know, a string is simply an array of characters. The below example:
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;is the same as:
char name[50];
std::cout << "Enter your name: ";
std::cin >> name;Though we can also allocate this dynamically:
char* name = new char[50];
std::cout << "Enter your name: ";
std::cin >> name;
// Some time later...
delete[] name; List of strings as a pointer to pointers
Now that we're able to express a string as a character pointer, we can further nest a list of strings as a pointer to multiple character pointers.
char* name_1 = new char[50];
std::cout << "Enter the first name: ";
std::cin >> name_1;
char* name_2 = new char[50];
std::cout << "Enter the second name: ";
std::cin >> name_2;
char** names = new char*[2]; // A dynamic array of two strings (two character pointers)
names[0] = name_1;
names[1] = name_2;
for (int i = 0; i < 2; i++) {
std::cout << names[i] << std::endl;
}Prompt
Create a simple to-do list program that stores a list of items to do as a list of strings.
The program should ask the user how many items they have on their to-do list, then asks the user to input each item. The program should then simply print out the items that the user has previously entered.
The program should store the list as an array of character pointers (
char**) as in the example above.Make sure that all dynamically allocated objects are deleted before the program ends.
Example:
How many items do you have to do? 4
Please enter the next item: Wash the clothes
Please enter the next item: Do the dishes
Please enter the next item: Finish homework
Please enter the next item: Go jogging
You have 4 tasks to do today!
- Wash the clothes
- Do the dishes
- Finish homework
- Go jogging Last updated
Was this helpful?