Programming Fundamentals
Lab Manual (Lab 13)
Topic: Relationship between Pointers and Arrays & Pointers to Functions
(CLO 1,2,3)
Course Instructor:
Lab Instructor:
Session: Fall 2024
School of Systems and Technology
Department of Computer Science
UMT Lahore Pakistan
Lab Objective:
By the end of this lab, students will:
• Understand the relationship between pointers and arrays.
• Use pointers to access and manipulate array data.
• Understand the concept of function pointers and how to use them in C++.
• Implement functions that take pointers as parameters and return pointers.
Scope:
This lab covers:
• The basics of arrays and how pointers relate to them.
• How pointers can be used to traverse and manipulate arrays.
• The concept of function pointers, including declaration, assignment, and usage.
• Writing and testing C++ programs that implement pointers with arrays and functions
Part 1: Relationship Between Pointers and Arrays
1. Introduction: In C++, an array name acts as a constant pointer to the first element of the
array. Pointers can be used to access elements of an array using pointer arithmetic.
2. Tasks:
o Declare an integer array with 5 elements.
o Declare a pointer and point it to the first element of the array.
o Use the pointer to traverse the array and print all elements.
o Modify an element of the array using the pointer and print the updated array.
3. Code Example:
cpp
Copy code
#include<iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // pointer points to the first element of the array
// Printing array elements using the pointer
for (int i = 0; i < 5; i++) {
cout << "Element " << i+1 << ": " << *(ptr + i) << endl;
}
// Modifying array using pointer
*(ptr + 2) = 100; // Update the third element
cout << "Updated array: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
Part 2: Pointers to Functions
1. Introduction: Pointers can also point to functions in C++. This allows you to pass functions
as parameters, return them from other functions, or store them in data structures.
2. Tasks:
o Write a function that accepts two integers and returns their sum.
o Create a pointer to this function.
o Call the function using the function pointer.
o Write a program that uses function pointers to switch between different arithmetic
operations (add, subtract, multiply).
3. Code Example:
cpp
Copy code
#include<iostream>
using namespace std;
// A simple function to add two integers
int add(int a, int b) {
return a + b;
}
int main() {
// Pointer to a function that takes two int parameters and returns int
int (*funcPtr)(int, int) = add;
// Call the function using the function pointer
int result = funcPtr(10, 20);
cout << "Result of addition: " << result << endl;
return 0;
}
4. Additional Challenge:
o Extend the program to include multiple functions like subtract, multiply, and divide.
Use a switch statement to allow the user to choose which operation to perform via
function pointers.
Lab Exercises:
1. Write a C++ program to use pointer arithmetic to find the maximum element in an array.
2. Write a C++ program to demonstrate how to pass a function pointer to another function.