Basic Pointers


  1. Write a C++ program that performs the following individual tasks. These tasks will combine to form a complete program.
    1. Define a constant integer size to have the value 5.
    2. Declare an array nums of size integers and initial values: 1, 2, 4, 8, 16
    3. Declare a pointer nptr to point to an integer.
    4. Write a for loop to print the values in the array nums.
      Solution to this point
    5. Make nptr point to nums.
    6. Print the value of nptr.
    7. 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
    8. Print the new value of nptr.

    9. Solution to this point
    10. Remove the loop that is bad practice.
    11. Write a function that receives a pointer to the numbers and reverses the numbers.
      void revrs( int *strt);
    12. 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;