For each of the following parts, write a single C++ statement that performs the indicated task. For each part, assume that all previous statements have been executed (e.g., when doing part e, assume the statements you wrote for parts a through d have been executed).
- Declare a pointer variable named fp that can point to a variable of type string.
- Declare fish to be a 5-element array of strings.
- Make the fp variable point to the last element of fish.
- Make the string pointed to by fp equal to “salmon”, using the * operator.
- Without using the fp pointer, and without using square brackets, set the element at index 3 of the fish array to have the value “yellowtail”.
- Move the fp pointer back by three strings.
- Using square brackets, but without using the name fish, set the element at index 2 of the fish array to have the value “eel”.
- Without using the * operator, but using square backets, set the string pointed to by fp to have the value “tuna”.
- Declare a bool variable named d and initialize it with an expression that evaluates to true if fp points to the string at the start of the fish array, and false otherwise.
- Using the * operator in the initialization expression, declare a bool variable named b and initialize it to true if the string pointed to by fp is equal to the string immediately following the string pointed to by fp, and false otherwise.
Problem 2
Assume the following variable declarations:
int bird = 0; int *binoculars = &bird;
Which of the following statements change the value of bird to 6?
- binoculars = 6;
- bird = 6;
- (*bird) = 6;
- (*binoculars) = 6;
- and b. only
- and d. only
- and d. only
- and d. only
Problem 3
Suppose you’re tasked with fixing a function definition that does not work as intended. The function is supposed to compare two strings and set the count to the number of identical characters, two characters are identical if they are the same character and are in the same position in the cstring. Note that cstrings are just character arrays that have ‘ ’ as their last character, for example
char name[7] = “harry”;
might looks like this in memory:
h | a | r | r | y |