PF-CE Lab10 Pointers
PF-CE Lab10 Pointers
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 10
Pointers & Pointer arrays
______________________________________
Lab Objectives:
1. To learn about 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;
}
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
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:
cout<<arr<<endl;
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
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].
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,
*ptr == arr[0];
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;
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 " << n << " elements:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
ptr = arr;
if (!found) {
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}
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;
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;
return 0;
}
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
// 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;
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