C CS265 Pointers and Dynamic Arrays Homework Pointer Basics

C++

CS265       Pointers and Dynamic Arrays Homework                  

Pointer Basics

Assume the following declarations:

            int i1 = 11, i2 = 22;

            double d1 = 3.55, d2 = 6.78;

1. Write declarations for variables p1 and p2 whose values will be addresses of memory locations in which a double can be stored

2. Write a statement to assign the addresses of d1 and d2 to the variables p1 and p2, respectively, in 1, or explain why this is not possible

3. Write a statement to assign the address of i2 to the variable p2 in 1, or explain why this is not possible

4. Write declarations that initialize variables ptr1 and ptr2 with the addresses of i1 and i2, respectively

5. Write a statement that will make variables p1 and p2 of problem 1 point to the same memory location

6. Write a statement that will copy the value stored in the memory location pointed to by ptr2 into the memory location pointed to by ptr1, for ptr1 and ptr2 as in problem 4

7. Write statements that use the variables p1 and p2 of problem 2, but not the variables d1 and d2, to interchange the values of d1 and d2

8. Using typedef, create an alias type CharPointer for pointers to type char.

9. Write a program to determine the address of memory locations allocated to various variables as follows:

Declare two integer variables, two char variables and two double variables and then output the address of each variable (see notes on the next page)

Calculate the size of the memory location allocated to each type by substracting the address of the first variable from the address of the second variable of the same type (see notes on the next page)

Add statements to your program that use the sizeof operator to find the size of the memory location allocated to each type (see notes on the next page)

Solution

1. Write declarations for variables p1 and p2 whose values will be addresses of memory locations in which a double can be stored.

Answer: double *p1, *p2;

2. Write a statement to assign the addresses of d1 and d2 to the variables p1 and p2, respectively, in 1, or explain why this is not possible.

Answer: *p1 = &d1;                      *p2 = &d2;

3. Write a statement to assign the address of i2 to the variable p2 in 1, or explain why this is not possible.

Answer: p2 = &i2; Not possible.

It will show an error message: cannot convert int * to double * in the assignment.

Because p2 is a double type pointer which can point to only double type variale, whereas i2 is of type integer.

4. Write declarations that initialize variables ptr1 and ptr2 with the addresses of i1 and i2, respectively.

Answer: int *ptr1 = &i1,    *ptr2 = &i2;

5. Write a statement that will make variables p1 and p2 of problem 1 point to the same memory location.

Answer: p1 = &d1;             p2 = &d1;

6. Write a statement that will copy the value stored in the memory location pointed to by ptr2 into the memory location pointed to by ptr1, for ptr1 and ptr2 as in problem 4.

Answer: *ptr1 = *ptr2;

7. Write statements that use the variables p1 and p2 of problem 2, but not the variables d1 and d2, to interchange the values of d1 and d2.

Answer: *p1 = *p1 + *p2;
               *p2= *p1 - *p2;
               *p1 = *p1 - *p2;

8. Using typedef, create an alias type CharPointer for pointers to type char.

Answer: typedef char* cp;

9. Write a program to determine the address of memory locations allocated to various variables as follows:

Declare two integer variables, two char variables and two double variables and then output the address of each variable (see notes on the next page)

Calculate the size of the memory location allocated to each type by substracting the address of the first variable from the address of the second variable of the same type (see notes on the next page)

Add statements to your program that use the sizeof operator to find the size of the memory location allocated to each type (see notes on the next page).

Answer:

#include<iostream>
using namespace std;
int main()
{
int iv1, iv2;
char cv1, cv2;
double dv1, dv2;
cout<<\"\ Address of iv1 = \"<<(unsigned)&iv1;
cout<<\"\ Address of iv2 = \"<<(unsigned)&iv2;
cout<<\"\ Address of cv1 = \"<<(unsigned)&cv1;
cout<<\"\ Address of cv2 = \"<<(unsigned)&cv2;
cout<<\"\ Address of dv1 = \"<<(unsigned)&dv1;
cout<<\"\ Address of dv2 = \"<<(unsigned)&dv2;

cout<<\"\ Address occupied Integer: \"<<(unsigned)iv1 - iv2;
cout<<\"\ Address occupied Character: \"<<(unsigned)cv1 - cv2;
cout<<\"\ Address occupied Double: \"<<(unsigned)dv1 - dv2;

cout<<\"\ Size of iv1 = \"<<sizeof(iv1);
cout<<\"\ Size of iv2 = \"<<sizeof(iv2);
cout<<\"\ Size of cv1 = \"<<sizeof(cv1);
cout<<\"\ Size of cv2 = \"<<sizeof(cv2);
cout<<\"\ Size of dv1 = \"<<sizeof(dv1);
cout<<\"\ Size of dv2 = \"<<sizeof(dv2);
}


Address of iv1 = 2293620
Address of iv2 = 2293616
Address of cv1 = 2293615
Address of cv2 = 2293614
Address of dv1 = 2293600
Address of dv2 = 2293592


Address occupied Integer: 2143089080
Address occupied Character: 4294967232
Address occupied Double: -7.96789e+268


Size of iv1 = 4
Size of iv2 = 4
Size of cv1 = 1
Size of cv2 = 1
Size of dv1 = 8
Size of dv2 = 8

C++ CS265 Pointers and Dynamic Arrays Homework Pointer Basics Assume the following declarations: int i1 = 11, i2 = 22; double d1 = 3.55, d2 = 6.78; 1. Write dec
C++ CS265 Pointers and Dynamic Arrays Homework Pointer Basics Assume the following declarations: int i1 = 11, i2 = 22; double d1 = 3.55, d2 = 6.78; 1. Write dec
C++ CS265 Pointers and Dynamic Arrays Homework Pointer Basics Assume the following declarations: int i1 = 11, i2 = 22; double d1 = 3.55, d2 = 6.78; 1. Write dec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site