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

PF-CE Lab10 Pointers

This laboratory manual for Programming Fundamentals (CL1002) focuses on pointers and pointer arrays, detailing their definitions, usage, and examples in C++. It includes tasks for students to implement various pointer-related programs, such as finding leaders in an array, searching for elements, applying a moving average filter, and analyzing pointer-to-pointer operations. The manual is prepared for BS-CE students at the National University of Computer and Emerging Sciences, Islamabad, for the Spring 2025 semester.

Uploaded by

mashnabsafdar
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

PF-CE Lab10 Pointers

This laboratory manual for Programming Fundamentals (CL1002) focuses on pointers and pointer arrays, detailing their definitions, usage, and examples in C++. It includes tasks for students to implement various pointer-related programs, such as finding leaders in an array, searching for elements, applying a moving average filter, and analyzing pointer-to-pointer operations. The manual is prepared for BS-CE students at the National University of Computer and Emerging Sciences, Islamabad, for the Spring 2025 semester.

Uploaded by

mashnabsafdar
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/ 9

Programming Fundamentals

(CL1002)

LABORATORY MANUAL
BS-CE, Spring 2025

LAB 10
Pointers & Pointer arrays

Muhammad Ashnab Safdar I24-6500 CE-A

STUDENT NAME ROLL NO SEC

______________________________________

LAB ENGINEER'S SIGNATURE & DATE


Muhammad Hammad

MARKS AWARDED: /10


_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir, Muhammad Hammad Version: 1.1
Date last
Last edited by: Engr. Azhar Rauf, Engr. Aamer Munir Sep 27, 2018
edited:
Date last
Verified by: Engr. Azhar Rauf Jan 10, 2025
edited:
Pointers & Pointer Arrays LAB 10

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 Pointers.

Software Required:
 Code: Dev C++

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 (*).

Example 1:

# 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:

PF LAB 10 NUCES, ISLAMABAD Page 2 of 9


Pointers & Pointer Arrays LAB 10
Example 2:

# 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
cout<<number<<endl;
// Using subscript .
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.

PF LAB 10 NUCES, ISLAMABAD Page 3 of 9


Pointers & Pointer Arrays LAB 10

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

PF LAB 10 NUCES, ISLAMABAD Page 4 of 9


Pointers & Pointer Arrays LAB 10

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.

Tasks
Task01:
Using Pointers only, write a program to print all the LEADERS in the array. An element
is leader if it is greater than all the elements to its right side. And the rightmost
element is always a leader. For example: in the array {16, 17, 4, 3, 5, 2, leaders are
17, 5 and 2.
#include <iostream>
using namespace std;

void findLeaders(int* arr, int size) {


int* ptr = arr + size - 1;
int max = *ptr;

cout << "Leaders: " << max << " ";

// Move from right to left


for (ptr = ptr - 1; ptr >= arr; ptr--) {
if (*ptr > max) {
max = *ptr;
cout << max << " ";
}
}

PF LAB 10 NUCES, ISLAMABAD Page 5 of 9


Pointers & Pointer Arrays LAB 10

cout << endl;


}

int main() {
int arr[] = {16,17, 4, 3, 5, 2};
int size = sizeof(arr) / sizeof(arr[0]);
findLeaders(arr, size);

return 0;
}

Task02:

Write a program to search for an element in an array using pointers. The program should
take an array, its size, and the element to be searched as input, and then use a pointer to
search for the element in the array.
#include <iostream>
using namespace std;

int main() {
int arr[100], n, key;
int* ptr;
bool found = false;

cout << "Enter the size of the array: ";


cin >> n;

cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Enter the element to search: ";


cin >> key;

ptr = arr;

for (int i = 0; i < n; i++) {


if (*(ptr + i) == key) {
cout << "Element " << key << " found at index " << i << "." << endl;
found = true;
break;
}
}

if (!found) {

PF LAB 10 NUCES, ISLAMABAD Page 6 of 9


Pointers & Pointer Arrays LAB 10
cout << "Element " << key << " not found in the array." << endl;
}

return 0;
}

Task03:

Scan the array A using Moving Average filter , M having window size ,w of 3.
A={1,2,4,20,7,1,30,40,50,30,4,2,1,15,50,40,50,60,30,20,2,3,5,20,100}

Updated value after scanning: A[i]= M = (A[i-1] + A[i] + A[i+1]) / 3.


Updated value after scanning: A[0]= M = (A[-1] + A[0] + A[1]) / 3.
Updated value after scanning: A[1]= M = (A[0] + A[1] + A[2]) / 3.

Write C++ code and do this by using pointers only. W can be input from user and 0’s can be
padded at end or start of the array to complete the window size.
ptr  A
#include <iostream>
using namespace std;

int main() {
const int originalSize = 25;
int A[originalSize] = {1, 2, 4, 20, 7, 1, 30, 40, 50, 30,
4, 2, 1, 15, 50, 40, 50, 60, 30, 20,
2, 3, 5, 20, 100};

int* ptr = A;
int w;

cout << "Enter window size (odd number): ";

PF LAB 10 NUCES, ISLAMABAD Page 7 of 9


Pointers & Pointer Arrays LAB 10
cin >> w;

if (w % 2 == 0 || w < 1) {
cout << "Window size must be an odd number greater than 0." << endl;
return 1;
}

int pad = w / 2;
int paddedSize = originalSize + 2 * pad;

int* padded = new int[paddedSize];

for (int i = 0; i < pad; i++) {


*(padded + i) = 0;
}

for (int i = 0; i < originalSize; i++) {


*(padded + pad + i) = *(ptr + i);
}

for (int i = originalSize + pad; i < paddedSize; i++) {


*(padded + i) = 0;
}

int* result = new int[originalSize];

for (int i = 0; i < originalSize; i++) {


int sum = 0;
for (int j = 0; j < w; j++) {
sum += *(padded + i + j);
}
*(result + i) = sum / w;
}

cout << "Array after applying moving average filter:\n";


for (int i = 0; i < originalSize; i++) {
cout << *(result + i) << " ";
}
cout << endl;

// Free allocated memory


delete[] padded;
delete[] result;

return 0;
}

PF LAB 10 NUCES, ISLAMABAD Page 8 of 9


Pointers & Pointer Arrays LAB 10

Task04:

Analyse the output of the following code which is using pointer to pointer

#include <iostream>
using namespace std;
int main() {
int var = 10;
int* ptr = &var; // pointer to var
int** ptrPtr = &ptr; // pointer to pointer

cout << "Value of var: " << var << endl;


cout << "Address of var: " << &var << endl;
cout << "Value of ptr: " << ptr << endl;
cout << "Address of ptr: " << &ptr << endl;
cout << "Value of ptrPtr: " << ptrPtr << endl;

// Arithmetic operations
cout << "Value of *ptr: " << *ptr << endl;
cout << "Value of **ptrPtr: " << **ptrPtr << endl;
*ptr = *ptr + (**ptrPtr);
cout<<" Value of var now is :"<<var<<endl;

// Modify var using ptrPtr


**ptrPtr = 30;
**ptrPtr+=1; //**ptrPtr++ doesnt work
cout << "Value of var after modification: " << var << endl;

return 0;
}
-----------------------------------------------------------------------------------------------------
Value of var: 10
Address of var: 0x5ffec4
Value of ptr: 0x5ffec4
Address of ptr: 0x5ffeb8
Value of ptrPtr: 0x5ffeb8
Value of *ptr: 10
Value of **ptrPtr: 10
Value of var now is :20
Value of var after modification: 31

PF LAB 10 NUCES, ISLAMABAD Page 9 of 9

You might also like