Basic Pointers
- Write a C++ program that performs the following individual tasks.
These tasks will combine to form a complete program.
- Define a constant integer size to have the value 5.
- Declare an array nums of size integers and
initial values: 1, 2, 4, 8, 16
- Declare a pointer nptr to point to an integer.
- Write a for loop to print the values in the array nums.
Solution to this point
- Make nptr point to nums.
- Print the value of nptr.
- Print the 5 numbers that nptr points to using pointer
arithmetic that modifies the value of nptr.
Note: This is NOT a good practice. There is no longer
a pointer to the start of the nums array
- Print the new value of nptr.
Solution to this point
- Remove the loop that is bad practice.
- Write a function that receives a pointer to the numbers and
reverses the numbers.
void revrs( int *strt);
- In the main program, print the reversed numbers using nptr and
pointer arithmetic that does NOT modify the value of nptr.
for (i=0; i< size; i++)
cout << *(nptr+i) << endl;