CSCI 261 - Programming Concepts - Maynooth 2022

Lab 3A - Pointers

This lab is due by Friday, July 08, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.


Pointer Instructions


The goal of this lab is to gain familiarity with using the concepts learned on pointers, addresses, and dynamic memory. Complete the following steps inside your main(). This lab will only be working with pointers on the stack. The next lab will begin using pointers to the heap.

  1. Declare an integer named iNum with initial value 9.
  2. Declare a double named dNum with initial value 14.25.
  3. Declare two pointers to integers named pINum1 and pINum2.
  4. Declare a pointer to a double named pDNum.
  5. Assign the address of iNum to pINum1.
  6. Output the address of iNum and be sure to identify to the user what you are displaying. There are two ways you can do this; you should do both, to convince yourself they are the same.
  7. Use pINum1 to display the contents of iNum.
  8. Try to assign the address of dNum to pINum2. Compile your program. What error message do you see? Comment out this bad line of code, but include the error message as a comment with this line.
  9. Assign the address of dNum to pDNum.
  10. Output the address and then the contents of dNum (using pDNum).
  11. Directly change the value of iNum to 7.
  12. Use pINum1 to output the contents of iNum.
  13. Assign pINum2 to have the same value as pINum1. Do not reference iNum; instead use the address stored in pINum1.
  14. Output the address in pINum2.  This should be the same as displayed in step 6.
  15. Output the value pointed to by pINum2.
  16. Using pINum2, change the contents of iNum to 12.
  17. Output the contents of iNum three times, first using pINum1, then using pINum2, then iNum directly. In each case, identify what the user is seeing appropriately.

Lab Submission


Submit your main.cpp file(s).

You will submit your solution to this lab with the rest of Set3. Detailed instructions for doing this are posted in Assignment 3.


This lab is due by Friday, July 08, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.