TCP Lab 04
TCP Lab 04
Objectives
To learn the usage of pointers
To differentiate between &ptr, ptr and *ptr
To learn basic DMA for singular variables and the concept of free store
Pointers
Declaring a pointer
Syntax: dataType *pointerName;
Eg: Declare an integer pointer called iPtr
int *iPtr;
Pointer manipulation
Eg:
int sum=77;
int *iPtr = ∑
cout << "Addr of sum: " << &sum << endl;
cout << "Addr of iPtr: " << &iPtr << endl;
cout << "Value of iPtr: " << iPtr << endl;
cout << "Value of variable pointed by iPtr: " << *iPtr << endl;
DMA
Uses the keywords
- new to allocate a space in the memory to store data
- delete to release the memory space held
1.1. The following program shows a simple pointer and variable assignment.
#include <iostream>
using namespace std;
int main()
{
int a;
int *aPtr;
aPtr = &a;
cout << "Enter an integer: ";
cin >> *aPtr; // input: 75
cout << "Address of a is " << &a << endl;
cout << "Address of aPtr is " << &aPtr << endl;
cout << "aPtr is pointing to " << aPtr << endl;
cout << "Value of a is " << a << endl;
cout << "Value of *aPtr is " << *aPtr << endl;
return 0;
}
Answer the following questions without compiling and running the program:
a) What are the outputs of the above program?
Address of a is 0x61fe1c
Address of aPtr is 0x61fe10
aPtr is pointing to 0x61fe1c
Value of a is 4
Value of *aPtr is 4
b) What is the difference between printing aPtr, &aPtr and *aPtr ?
aPtr address that the pointer pointed to
&aPtr address of the pointer itself
*aPtr value that the pointer pointed to
c) What is the relationship between a and *aPtr?
a and *aPtr both are accessing the same memory location
1.2. This program shows that operations can be done on dereferenced pointer variables.
#include <iostream>
using namespace std;
int main()
{
int a = 3, b;
int *aPtr = &a,
*bPtr = &b;
*bPtr = (*aPtr)++;
cout << "Value of b is " << b << "\n"; // output C
cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output D
*bPtr = ++*aPtr;
cout << "Value of b is " << b << "\n"; // output E
cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output F
return 0;
}
Answer the following questions without compiling and running the program:
a) What are the outputs of A, B, C, D, E and F?
Value of b is 903
Value of *bPtr is 903
Value of b is 3
Value of *bPtr is 3
Value of b is 5
Value of *bPtr is 5
Question 2
2.1. This program dynamically creates and destroys variables at run-time using the operators new
and delete. Observe this program closely.
#include <iostream>
using namespace std;
int main()
{
int *p1, *p2;
p1 = new int;
*p1 = 42;
p2 = p1;
cout << "*p1 == " << *p1 << "\n";
cout << "*p2 == " << *p2 << "\n\n";
*p2 = 53;
cout << "*p1 == " << *p1 << "\n";
cout << "*p2 == " << *p2 << "\n\n";
return 0;
}
a) What actually happens here? Think of new and delete as operators for dynamically creating
and destroying data variables in the “free store” of the memory. Dynamic data variables are
referenced with pointers.
*p1 == 42
*p2 == 42
*p1 == 53
*p2 == 53
*p1 == 88
*p2 == 53
*p1 == 42
*p2 == 42
*p1 == 53
*p2 == 53
*p1 == 88
*p2 == 88
b) Does the line delete p1 mean that the pointer p1 has been deleted?
No. it is for free up the storage.
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
int *p1, *p2;
p1 = &a;
p2 = &b;
cout << "*p1 = " << *p1 << endl;
cout << "*p2 = " << *p2 << endl;
int *p3;
*p3 = 15;
cout << "*p3 = " << *p3 << endl;
return 0;
}
a) What do you think caused the run-time (dialog box) error (no output for *p3)?
*p3 cannot store constant
b) Modify the program above to allow the use of a dynamic data variable using the operator
new so that the value of *p3 is shown.
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
int *p1, *p2;
p1 = &a;
p2 = &b;
cout << "*p1 = " << *p1 << endl;
cout << "*p2 = " << *p2 << endl;
int *p3 = new int;
*p3 = 15;
cout << "*p3 = " << *p3 << endl;
return 0;
}
Question 3
Try to use the concept of pointers and complete the below C++ program for selection sort.
#include <iostream>
using namespace std;
int main()
{ const int MAX = 10;
int array[MAX] = {8, 6, 10, 2, 16, 4, 18, 14, 12, 20};
/*smallest pointer is used to point to the smallest element in
the unsorted list, and,
current pointer is used to point to the current position to be
sorted*/
int *smallest, *current = array;
for(int i=0; i<MAX; i++, current++){
smallest = current;
cout << "\nCurrent=" << *current;
//search for smallest element in the unsorted list
// WRITE YOUR CODE
}
//display the final sorted array
// WRITE YOUR CODE
return 0;
}