You are on page 1of 8

Object Oriented Programming Lab

(CL1004)
LABORATORY MANUAL
Fall 2023

LAB 04
Pointer Basics & Pointers Arrays
Engr. Arslan Ahmed

________________________________________ __________ ___


STUDENT NAME ROLL NO SEC

______________________________________
LAB ENGINEER SIGNATURE & DATE

MARKS AWARDED: /10

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES),


ISLAMABAD
Pointer Basics & Pointers Arrays LAB 04

LAB 04 Pointer Basics & Pointers Arrays

Lab Objectives:
1. To learn about pointers.
2. To learn the difference and similarity between pointers and arrays.
3. To learn about different types of Pointer.
4. Learn the method of passing pointers to functions.
5. To learn the difference and similarity between pointers and arrays.
6. Dynamic Memory Allocation
7. To learn the method of passing pointers to functions.

Software Required:
• Dev C++ / Visual Studio Code

Introduction:
1. Pointers
A pointer is a variable that stores address of another variable. This pointer essentially
points to that variable. Pointer can be used to access and change the value of variable
it points to, by using the dereference operator (*).

# include<iostream>
using namespace std;

int main ()
{
int a=3; // A simple int .
int* ap; // A pointer of type int .
ap = &a; // Pointer ap now points to int a.
*ap = 5; // Changes value of a to 5 from 3.
cout<< a <<endl ; // Print value of a.
cout<<&a <<endl ; // Print address of a.
cout<<ap<<endl ; // Print value of ap , which is address of a.
cout<< *ap<<endl ; // Print value of a using pointer ap.

return 0;
}

2. Pointers and Arrays


The name of an array is the pointer to its first element. A subscript besides array name
can be translated in English as, “access element located this much distance from start."
Keep in mind that Array[i] is the same as *(Array+i) which means that you only need
array pointer to access array elements using the subscript. Consider the following
code:
# include<iostream>
using namespace std;

int main ()
{
char Array [6] = {'3' ,'7' ,'2' ,'1' ,'5'}; // 5 characters and 1 NULL
int number[2][2]={{1,2},{3,4}};
cout<< Array <<endl ; // Prints the complete array
// Using subscript .

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 2 of 8


Pointer Basics & Pointers Arrays LAB 04

cout<< Array [0] <<endl ; cout<< Array [3] <<endl ;


cout<< Array [5] <<endl ; cout<< Array [6] <<endl ;
// Using Pointers .
cout<< *(Array +0) <<endl ; // Prints the value of first element which is 3.
cout<< *(Array +1) <<endl ; // Prints the value of second element which is 7.
cout<< *(Array +5) <<endl ; // Prints the value of fifth element which is NULL
cout<< *(Array +6) <<endl ; // Prints the value of sixth element which is out
of bound or ERROR .
return 0;
}

Not only can a pointer store the address of a single variable, it can also store the starting

address of an array.

Method 1:

Consider this example:

int arr [5];

cout<<arr<<endl;

/* Display the address of the first element of arr that is address of


arr[0]. Here arr is used as a pointer to store the starting address of
an array. */

Method 2:
int *ptr;
int arr[5];

ptr = arr;
// store the address of the first element of arr in ptr

Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the

address of the first element of the array in variable ptr.

Notice that we have used arr instead of &arr[0]. This is because both are the same.
The addresses for the rest of the array elements are given by &arr[1], &arr[2], &arr[3],

and &arr[4].

Point to Every Array Elements

Suppose we need to point to the fourth element of the array using the same pointer ptr.

Here, if ptr points to the first element in the above example then ptr + 3 will point to the

fourth element. For example,


int *ptr;

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 3 of 8


Pointer Basics & Pointers Arrays LAB 04

int arr[5];
ptr = arr;
ptr + 1 is equivalent to &arr[1];
ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];

Similarly, we can access the elements using the single pointer. For example,

// use dereference operator

*ptr == arr[0];

*(ptr + 1) is equivalent to arr[1];

*(ptr + 2) is equivalent to arr[2];

*(ptr + 3) is equivalent to arr[3];

*(ptr + 4) is equivalent to arr[4];

Suppose if we have initialized ptr = &arr[2]; then

ptr - 2 is equivalent to &arr[0];

ptr - 1 is equivalent to &arr[1];

ptr + 1 is equivalent to &arr[3];

ptr + 2 is equivalent to &arr[4];

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 4 of 8


Pointer Basics & Pointers Arrays LAB 04

Note: The address between ptr and ptr + 1 differs by 4 bytes. It is because ptr is a

pointer to an int data. And, the size of int is 4 bytes in a 64-bit operating system.

Similarly, if pointer ptr is pointing to char type data, then the address

between ptr and ptr + 1 is 1 byte. It is because the size of a character is 1 byte.

Dynamic Memory Allocation

C++ allows us to allocate the memory of a variable or an array in run time. This is known as

dynamic memory allocation.

We can allocate and then deallocate memory dynamically using the new and delete operators

respectively as demonstrated in the following example.

// Example program for dynamic memory allocation


#include <iostream>
using namespace std;

int main()
{
int i, n;
int *arr;
cout << "How many numbers would you like to type? " << endl;
cin >> n;
arr = new int[n];
for (i = 0; i < n; i++)
{
cout << " Enter number " << i + 1 << " : " << endl;
cin >> arr[i];
}
cout << "You have entered following numbers : " << endl;
for (i = 0; i < n; i++)
cout << arr[i] << ", ";
delete[] arr;
arr = NULL;
return 0;
}

3. Pass by Pointers
C++ allows to pass a pointer to a function. Then the parameter has to be declared as
a pointer type

# include<iostream>
using namespace std;

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 5 of 8


Pointer Basics & Pointers Arrays LAB 04

int Area (int *, int *); // Function prototype.

int main ()
{
int a, b, c;
a = 3;
b = 5;
c = Area (&a, &b); // Function call by address.
cout<<"Area of "<< a <<"*"<< b <<" is: "<< c <<endl;
return 0;
}

// Function definition .
int Area (int * x, int * y)
{
int z = (*x)*(* y); // Dereferencing pointers .
*x=7; // Changes value of a in main ().
return z;
}
Since pointers point to original variables they can be used to change the value of
original variables in a function call.
To pass array in pointer notation to function, see following example:

#include <iostream>
using namespace std;

// function declaration:
double getAverage(int *arr, int size);

int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;

// pass pointer to the array as an argument.


avg = getAverage( balance, 5 ) ;

// output the returned value


cout << "Average value is: " << avg << endl;

return 0;
}
double getAverage(int *arr, int size)
{
int i, sum = 0;
double avg;

for (i = 0; i < size; ++i)


{
sum += arr[i];
}
avg = double(sum) / size;

return avg;
}

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 6 of 8


Pointer Basics & Pointers Arrays LAB 04

Lab Tasks:
1. Dry Run this code:

#include <iostream>
using namespace std;
int main() { // Missing parentheses after "main"
int set[5] = {15, 30, 45, 60, 75};
int w = 10;
char* ch;
float* fl;
double* db;
int* ptr = &w;
int* ptr1;
char* ptr2;
float* ptr3;
double* ptr4;
int i = 1;
char c = 'a';
float f = 3.12;
double d = 3.1234;
db = &d;
fl = &f;
ptr = set; // "ptr" is already declared above, so remove "int"
cout << *ptr << endl;
for (int q = 3; q < 5; q++) {
cout << *(ptr + q) << endl; // Use "q" instead of incrementing "ptr"
}
cout << *(ptr + 2) << endl;
cout << *(ptr + 3) << endl;
cout << db << "\t" << *fl << endl; // Use "*fl" instead of "f1"
ptr1 = &i;
ptr2 = &c;
ptr3 = &f;
ptr4 = &d;
cout << *ptr1 << *ptr2 << *ptr3 << *ptr4 << endl; // Add "*" before each pointer variable
return 0; // Add a return statement
}

2. Declare an array of fixed size as arr [5]. Show the address of each memory location using
pointer.

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 7 of 8


Pointer Basics & Pointers Arrays LAB 04

3. Take an alphabet either lowercase or uppercase from user, and store the next characters
in two arrays. The two arrays should be created dynamically with size only as much as
needed. At the end print the arrays as shown below.

Fall 2023 OOP Lab NUCES, ISLAMABAD Page 8 of 8

You might also like