Lab05

**CS 215 Summer 2012** **Lab 05** **"Pointers and Indirection"**

Put your program and your answers to the exercises in a .zip file and upload it to the CS Portal.
**Goals and Objectives**

This week’s laboratory is intended to familiarize you with pointers and how they are used in the C++ programming language.
==Previously, you have learned that in C++ there is a close relationship between every data type and the underlying memory structure. Data is mapped to a fixed number of linearly numbered (addressed) memory bytes. In C++, we can take advantage of this by having a variable that contains the “address” or memory location of the value of a data element instead of the value itself. This is called “indirection” and we refer to this type of variable as a “pointer”. ==

__**Exercise 1** __
==Write a small program that declares and initializes four variables (an int, double, char and string). Using the information from your lectures and reading, make the program output the values of each of the variables and the address of each of the variables. ==

char letter; letter='i'; cout<(&letter);

__**Exercise 2** __
==Write a small program that declares four pointer variables (an int*, double*, char* and string*). Using dereferencing, initialize each of the pointers to an appropriate value. Have the program output the contents of each of pointers. Then have the programs output the values that the pointers refer to. ==

__**Exercise 3** __
==Write a small program that declares five pointer variables (a short, int, float, double and string). Write the program to display the contents of each of the pointers. Next, increment each pointer by 1 and then display the contents of each of the pointers again. ==

__**Exercise 4** __
==Using what you have learned from this lab so far, write a program that declares an array of ten ints and then loads that array with values (you can use a formula like, for example, perfect squares, or random numbers). After this step, write the code that prints out the contents of the array //without using any subscripting//, i.e., without using brackets [] and subscripts. ==