Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 12: Pointers & More Last Modified 03/2/2016 Key Concepts: Pointers and addre

ID: 3686357 • Letter: L

Question

Lab 12: Pointers & More

Last Modified 03/2/2016

Key Concepts: Pointers and addresses

This lab is different from most we've done so far: it's not about creating a program for an end-user to achieve some functional purpose, but instead about exploring a new concept. In that spirit, it doesn't need a proper data dictionary or internal documentation for the sake of maintenance. Instead, it needs documentation just to organize it.

It has multiple parts.

Part I

Yesterday in class, in Part VII, we did some basic pointer exercises. Create a C++ program where you put the code for each of the five exercises in a main program. Use full-line comments to tell when each new exercise starts.

After each step, print out the following:

The value of each of named variable

The address of each named variable

The value of each pointer's pointee, if it exists

The address stored in each pointer (i.e. print the pointer without the derefencing operator)

Your output should look something like this:

You should gain some appreciation for how pointers store addresses and how the addresses are really working from this exercise.

Part II

Now, try some other things and respond to these items on paper:

On one hand, hopefully you followed the directions in printing pointees when the directions told you print them only if they existed. On the other hand, hopefully you didn't. In any event, what happens if you try to print out a pointee that doesn't exist? Try doing this in your program and write down what happens. (Remember what happened to Binky?)

Create an array of 10 integers, and print out the name of the array without the subscript operator, e.g. for int a[10], print a. What do you think is going on?

Initialize the elements of your array with a loop to anything you want. Then execute these lines of code: cout << *(a + 1); and cout << *(a + 7); . What do you think is happening?

Create a void method with a parameter that's passed by reference and a local variable whose name is the same as one of your variables existing in main. In the method, print out the addresses of both the local variable and the parameter. In main, print out the addresses of both of your variables again, then call the method with one as a parameter. Compare the addresses. Do variables with the same name have the same addresses? Why or why not? How do the address of the parameter and its argument compare? Why does that make sense?

Explanation / Answer

// Example program

#include <string>

#include <iostream>

using namespace std;

int main ()

{

   double x = 1.25;   // actual variable declaration

   double y=2.5;

   double *ptr;

    double *ptr1;

   double **pptr1;

   double **pptr2;

   double k=5;

      /* take the address of var */

   ptr = &y;

   /* take the address of ptr using address of operator & */

   pptr1 = &ptr;

   ptr1=&k;

       pptr2 = &ptr1;

   // pointer variable

       // store address of var in pointer variable

   cout << "Value of x variable is: ";

   cout << x << endl;

   // print the address stored in ip pointer variable

   cout << "Address stored in x variable: ";

   cout << &x << endl;

cout << "Value of y variable is: ";

   cout << y << endl;

   // print the address stored in ip pointer variable

   cout << "Address stored in y variable: ";

   cout << &y << endl;

   // access the value at the address available in pointer

   cout << "Value of *ip1 variable: ";

   cout << **pptr1<< endl;

    cout << "adress of *ip1 variable: ";

   cout << &ptr<< endl;

    cout << "Value of *ip2 variable: ";

   cout << **pptr2<< endl;

    cout << "adress of *ip2 variable: ";

   cout << &ptr1<< endl;

   return 0;

}

Sample output::

Value of x variable is: 1.25

Address stored in x variable: 0x7a03c7a504a8

Value of y variable is: 2.5

Address stored in y variable: 0x7a03c7a504b0

Value of *ip1 variable: 2.5

adress of *ip1 variable: 0x7a03c7a504b8

Value of *ip2 variable: 5

adress of *ip2 variable: 0x7a03c7a504c0

In any event, what happens if you try to print out a pointee that doesn't exist? Try doing this in your program and write down what happens. (Remember what happened to Binky?)

Answer is::in this iam not declare the pointer3 but I am displaying.

#include <string>

#include <iostream>

using namespace std;

int main ()

{

   double x = 1.25;   // actual variable declaration

   double y=2.5;

   double *ptr;

    double *ptr1;

   double **pptr1;

   double **pptr2;

   double k=5;

      /* take the address of var */

   ptr = &y;

   /* take the address of ptr using address of operator & */

   pptr1 = &ptr;

   ptr1=&k;

       pptr2 = &ptr1;

   // pointer variable

       // store address of var in pointer variable

   cout << "Value of x variable is: ";

   cout << x << endl;

   // print the address stored in ip pointer variable

   cout << "Address stored in x variable: ";

   cout << &x << endl;

cout << "Value of y variable is: ";

   cout << y << endl;

   // print the address stored in ip pointer variable

   cout << "Address stored in y variable: ";

   cout << &y << endl;

   // access the value at the address available in pointer

   cout << "Value of *ip1 variable: ";

   cout << **pptr1<< endl;

    cout << "adress of *ip1 variable: ";

   cout << &ptr<< endl;

    cout << "Value of *ip2 variable: ";

   cout << **pptr2<< endl;

    cout << "adress of *ip2 variable: ";

   cout << &ptr1<< endl;

   // print the addres and value of pointer which does not exit

      cout << "Value of *ip3 variable: ";

   cout << **pptr3<< endl;

    cout << "adress of *ip2 variable: ";

   cout << &ptr3<< endl;

   return 0;

}

SAMPLE OUTPUT::

In function 'int main()':

50:14: error: 'pptr3' was not declared in this scope

52:13: error: 'ptr3' was not declared in this scope

   Create an array of 10 integers, and print out the name of the array without the subscript operator, e.g. for int a[10], print a. What do you think is going on?

Initialize the elements of your array with a loop to anything you want. Then execute these lines of code: cout << *(a + 1); and cout << *(a + 7); . What do you think is happening?

Answer for the above question is

#include <iostream>

using namespace std;

const int MAX = 10;

int main ()

{

   int a[MAX] = {1, 2,3,4,5,6,7,8,9,10};

      for (int i = 0; i < MAX; i++)

   {

      cout << "Value of var= ";

      cout << a<< endl;

      cout << *(a + 1)<< endl;

      cout << *(a + 7)<< endl;

   }

   return 0;

}

Sample output::

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Value of var= 0x7b87628c18a0

2

8

Exit code: 0 (normal program termination)

Create a void method with a parameter that's passed by reference and a local variable whose name is the same as one of your variables existing in main. In the method, print out the addresses of both the local variable and the parameter. In main, print out the addresses of both of your variables again, then call the method with one as a parameter. Compare the addresses. Do variables with the same name have the same addresses? Why or why not? How do the address of the parameter and its argument compare? Why does that make sense?

Answer. #include <iostream>

using namespace std;

// function definition to swap the values.

void swap(int &x, int &y)

{

   int temp;

   temp = x; /* save the value at address x */

   x = y;    /* put y into x */

   y = temp; /* put x into y */

   return;

}

// function declaration

void swap(int &x, int &y);

int main ()

{

   // local variable declaration:

   int x = 100;

   int y = 200;

   cout << "Before swap, value of x :" << x << endl;

   cout << "Before swap, value of y :" << y << endl;

   /* calling a function to swap the values using variable reference.*/

   swap(x, y);

   cout << "After swap, value of x :" << x << endl;

   cout << "After swap, value of y :" << y << endl;

    cout << "After swap, adress of x :" << &x << endl;

cout << "After swap, adress of y :" << &y << endl;

   return 0;

}

Sample output::

Before swap, value of x :100

Before swap, value of y :200

After swap, value of x :200

After swap, value of y :100

After swap, adress of x :0x770ad671c2d8

After swap, adress of y :0x770ad671c2dc