0% found this document useful (0 votes)
4 views

TCP Lab 04

This document provides an overview of pointers in C++, including their declaration, manipulation, and dynamic memory allocation (DMA). It includes example programs demonstrating pointer usage, the relationship between pointers and variables, and how to dynamically create and delete variables. Additionally, it presents a selection sort implementation that requires the use of pointers to sort an array.

Uploaded by

chloesim178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

TCP Lab 04

This document provides an overview of pointers in C++, including their declaration, manipulation, and dynamic memory allocation (DMA). It includes example programs demonstrating pointer usage, the relationship between pointers and variables, and how to dynamically create and delete variables. Additionally, it presents a selection sort implementation that requires the use of pointers to sort an array.

Uploaded by

chloesim178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 04: Pointers

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;

Declaring and assigning a pointer to a variable


Syntax: dataType *pointerName = &variableName;
Eg: Declare an integer pointer called iPtr and point it to the variable sum
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;

Observe the difference in the iPtr used:


- & for the address of where the iPtr pointer is kept in memory
- pointer name for the value that is being kept by the iPtr pointer. Naturally will be the address of
the variable that iPtr is pointing to
- * for dereferencing the iPtr pointer to goto the variable it’s pointing to and retrieve the value

DMA
Uses the keywords
- new to allocate a space in the memory to store data
- delete to release the memory space held

Creating a single dynamic variable


Syntax: dataType *varName = new dataType;
Eg: dynamically create an integer variable called tmp
int *tmp = new int;
Question 1

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 + 900;


cout << "Value of b is " << b << "\n"; // output A
cout << "Value of *bPtr is " << *bPtr << "\n\n"; // output 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

b) Explain how do you get the outputs?

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";

// delete p1; // Enable this in the second run,


// and compare the results.
p1 = new int;
*p1 = 88;
cout << "*p1 == " << *p1 << "\n";
cout << "*p2 == " << *p2 << "\n\n";
cout << "Hope you got the point using this example!\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.

2.2. Given the following program.

#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

cout << " Smallest=" <<*smallest<< endl;


//swap smallest and current
// WRITE YOUR CODE

//display the modified array after swap


// WRITE YOUR CODE

}
//display the final sorted array
// WRITE YOUR CODE

return 0;
}

You might also like