For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory. a) Declare an array of type int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5. b) Declare a pointer vPtr that points to an integer variable. c) Use a for statement to print the elements of array values using array subscript notation. d) Write a statement that assign the starting address of array values to pointer variable vPtr. e) Use a for statement to print the elements of array values using pointer/offset notation. f) What address is referenced by vPtr + 3? What value is stored at that location? g) Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location? |
int values[5]{2, 4, 6, 8, 10};
|
|
|
|
using array subscript notation |
array[i]
. The '[i]' is the subscript.
|
|
vPtr = values[5]
won't work. You're trying to put an integer into a pointer of type int (pointers hold memory addresses, not integers). Think about how you can retrieve the memory location of a variable (hint, look at the code above).values[4]
if you want to access the 5th element in the array.