You are on page 1of 14

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-13D
Fall 2022

Lab04: Pointers

Date: 3rd October, 2022

Name:Abdurrahim

Reg no:369247
Lab Objective:
To have better understanding of pointers

Lab Description:
Pointers are powerful features of C++ that differentiates it from other programming languages like
Java and Python. Pointers are used in C++ program to access the memory and manipulate the
address.

Address in C++
To understand pointers, you should first know how data is stored on the computer.

Each variable you create in your program is assigned a location in the computer's memory.
The value the variable stores is actually stored in the location assigned.

To know where the data is stored, C++ has an & operator. The &(reference)operator gives
you the address occupied by a variable.

If var is a variable then, &var gives the address of that variable.

Activity 01:
In the following example, you may not get the same result on your system. The 0x in the
beginning represents the address is in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs from third
by 4-bytes. This is because the size of integer (variable of type int) is 4 bytes in 64-bit
system.

Code OUTPUT

CS 212: Object Oriented Page 2


Programming
Pointers Variables
C++ gives you the power to manipulate the data in the computer's memory directly. You can
assign and de-assign any space in the memory as you wish. This is done using Pointer
variables.
Pointers variables are variables that points to a specific address in the memory pointed by
another variable.

How to declare a pointer?


int *p
Or,
int* p

The statement above defines a pointer variable p. It holds the memory address.

The asterisk is a dereference operator which means pointer to. Here, pointer p is a pointer to
int, i.e., it is pointing to an integer value in the memory address.

Reference operator (&) and Deference operator (*)


Reference operator (&) as discussed above gives the address of a variable. To get the value
stored in the memory address, we use the dereference operator (*).

For example: If a number variable is stored in the memory address 0x123, and it contains a
value 5.

The reference (&) operator gives value 0x123, while the dereference (*)
operator gives the value 5.

The (*) sign used in the declaration of C++ pointer is not the dereference pointer. It is just a
similar notation that creates a pointer.

CS 212: Object Oriented Page 3


Programming
Activity 02:
The following program demonstrates the working of pointer

The “void” type pointer:


void * p;

The pointer variable “p” can hold the memory address of variable of any data type.

Pointers and Arrays:


There is a close relationship between pointers and arrays, when an array is declared the array
name is the starting address of the array. For example
int x[5]; int * p; p=x;

CS 212: Object Oriented Page 4


Programming
Common mistakes when working with pointers

Suppose, you want pointer pc to point to the address of c. Then,

int c, *pc;
2. pc=c; /* Wrong! pc is address whereas, c is not an address.*/

3. *pc=&c; /* Wrong! *pc is the value pointed by address whereas,


&c is an address*/
4. pc=&c; /* Correct! pc is an address and, &pc is also an address */
5. *pc=c; /* Correct! *pc is the value pointed by address and c is also a
value. */

Call by Reference
You learned about passing arguments to a function. This method used is called passing by value
because the actual value is passed.
However, there is another way of passing an argument to a function, where the actual value of the
argument is not passed. Instead, only the reference to that value is passed.

Think??
 T1. Point out error(s) if any in the following programs.

CS 212: Object Oriented Page 5


Programming
 T2. What is the output of the following programs?

(A) (B) (C)

CS 212: Object Oriented Page 6


Programming
Task 1:
Write a program in C++ to swap elements using call by reference. Define a function swap.
Pass the values to the function for swapping. Print the values before and after swapping
through main.
Test Data:
Input the value of 1st element: 5
Input the value of 2nd element: 6
Input the value of 3rd element: 7
Expected Output:
The value before swapping are:
element 1 = 5
element 2 = 6
element 3 = 7

The value after swapping are:


element 1 = 7
element 2 = 5
element 3 = 6

Code:
#include<iostream>
using namespace std;
//--------------------------------------------------------------------------------
void swap(int&x, int & , int &); // decleration protype
void print(int a, int b, int c) {
cout << " the numbers of element 1" << ":";
cout << a << endl;
cout << " the numbers of element 2" << ":";
cout << b << endl;
cout << " the numbers of element 3" << ":";
cout << c << endl;
}

//-------------------------------------------------------------------------------------------
int main() {
int a,b,c;

CS 212: Object Oriented Page 7


Programming
cout << "enter the numbers of element 1" << ":";
cin >> a;
cout << "enter the numbers of element 2" << ":";
cin >> b;
cout << "enter the numbers of element 3" << ":";
cin >> c;
cout << "the numbers before swapping" << endl << endl;
print(a, b, c);
cout << "the numbers after swapping" << endl<<endl;

swap(a,b,c);
print(a, b, c);
return 0;
}
//--------------------------------------------------------------------------
void swap(int &x, int &y, int &z) { // decleration protype
int temp1, temp2;
temp1 = x;
temp2 = y;
x = z;
y = temp1;
z = temp2;
}

Output:

CS 212: Object Oriented Page 8


Programming
Task 2:
Write a C++ program to accept five integer values in an array using pointer offset notation.
Print the array as an output.

Code:
#include<iostream>
using namespace std;
//--------------------------------------------------------------------------------
int main() {
int array[5]; // declaring type to array
for (int i = 0; i < 5; i++) {
cout << "ENter the number " << i + 1 << ":";
cin >> *(array + i); // userdefined values by pointers
}
cout << endl << endl; // for space
for (int i = 0; i < 5; i++) {
cout << "the number " << i + 1 << ":";
cout << *(array + i)<<endl;
CS 212: Object Oriented Page 9
Programming
}
}

Output:

CS 212: Object Oriented Page


Programming 10
Task 3
Write a program in C++ using following functions which initializes an array with random numbers
(Assume N= 10), print and sort the array using two different functions. Define a function find to
find certain element in the array. You must use the 4 functions mentioned below. You must pass
array to functions using pointers.

1. void populate_array(int *array, int N);


// this initializes an array of size N with random values.

2. void print_array(int *array, int N);


// this function prints the entire array.

3. void sort (int *array, int N);


//sort the elements of array in descending order
CS 212: Object Oriented Page
Programming 11
4.bool find (int *array, intx, int N);
//find the element in the array

CODE:
#include<iostream>
#include<cstdlib> // for srand and rand
#include<time.h>// for time
using namespace std;
//--------------------------------------------------------------------------------
void populate_array(int* array, int N);
// this initializes an array of size N with random values.

void print_array(int* array, int N);


// this function prints the entire array.

void sort(int* array, int N);


//sort the elements of array in descending order

bool find(int* array, int x, int N);


//find the element in the array

int main() {
int a[10], x;
populate_array(a,10);
print_array(a, 10);
sort(a, 10);
cout << endl << "after sorting" << endl;
print_array(a, 10);
cout << endl << endl << "eneter the number:";
cin >> x;
if ((find(a, x, 10)))
cout << "it is present";
else
cout << "it is not present";

}
void populate_array(int* array, int N)
{
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++) {
*(array + i) = rand()%20;

}
}
///////////////////////////////////////////////////////////////
void print_array(int* array,int N)
{
for (int i = 0; i < N; i++) {
cout << "the value of output is " << i + 1 << ":"<< *(array + i)<<endl;

CS 212: Object Oriented Page


Programming 12
}
}
//////////////////////////////////////////////////////////////////////////////
bool find(int* array, int x, int N) {
for (int i = 0; i < N; i++) {
if (x == *(array + i))
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
void sort(int* array, int N) {
int temp;
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < N; j++) {
if (*(array + i) < *(array + j)) {
temp = *(array + i);
*(array + i) = *(array + j);
*(array + j) = temp;
}
}
}
}
//////////////////////////////////////////////////////////

CS 212: Object Oriented Page


Programming 13
Output:

Your main should just have the function calls, input for finding a number and one statement for showing if
number exists in the array or not.

Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS in a single word file.
CS 212: Object Oriented Page
Programming 14

You might also like