PF Lab 09 Manual
PF Lab 09 Manual
Lab 09
POINTERS, POINTERS & FUNCTIONS, POINTERS & FUNCTIONS, POINTERS & 1D
Topic
ARRAYS, POINTERS, 1D ARRAYS & FUNCTIONS
Practical implementation of the following topics
• POINTERS
• POINTERS & FUNCTIONS
Objectives • POINTERS & ARRAYS
• POINTERS, ARRAYS & FUNCTIONS
❖ POINTERS:
A pointer however, is a variable that stores the memory address as its value. A pointer variable points to
a data type (like int or string) of the same type, and is created with the * operator. The address of the
variable you're working with is assigned to the pointer.
POINTERS Declaration:
Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk
sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're
working with. Use the & operator to store the memory address of the variable called food, and assign it
to the pointer. Now, ptr holds the value of declared array in memory address.
There are three ways to declare pointer variables, but the first way is preferred:
❖ 1D-Array:
An array is a collection of a fixed number of components (also called elements) all of the same data type
and in contiguous (that is, adjacent) memory space. An array whose components are of type char.
char list[10];
Above statement is used for creating an array. But as we discussed above when a memory location is
reserved for this array it holds some values which are not assigned by user. So those values are considered
1
as garbage value. In order to avoid garbage value, it is a good practice to assign values at the time of
creation of array.
Assigning value at the time of declaring of array or variable is called initialization statement. There are
multiple ways of initialize an array.
char list[5]={‘a’,’b’,’c’,’d’,’e’};
char list[5]={‘a’,’b’};
on remaining indexes null will be assigned as initial value in case of partial array initialization.
❖ Constant pointer
In C++, a constant pointer is a pointer that points to a memory location whose value cannot be changed.
This means that once a constant pointer is assigned a memory address, it cannot be reassigned to point
to a different memory location. However, the value stored in the memory location being pointed to can
still be modified.
#include <iostream>
int main() {
std::cout << "Value: " << *ptr << std::endl; // Output: Value: 42
2
*ptr = 99; // Modifying the value through the constant pointer
std::cout << "Value: " << *ptr << std::endl; // Output: Value: 99
// ptr = &anotherValue;
return 0;
In this example, we declare a constant pointer ptr using the int* const syntax, indicating that ptr will
always point to an integer. We assign it the memory address of the variable value. Since ptr is a constant
pointer, it cannot be reassigned to point to a different memory location.
We then use *ptr to modify the value stored at the memory location it points to. In this case, we change
the value of value from 42 to 99. However, we cannot reassign ptr to point to the anotherValue variable
because ptr is declared as a constant pointer. If we uncomment the line that attempts to reassign ptr, a
compilation error will occur.
❖ Pointer to Arrays
A pointer to an array is a way to refer to an entire array using a single pointer variable. It allows you to
manipulate and access elements of an array using pointer arithmetic.
Here's an example:
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* ptr = arr; // Pointer to the first element of the array
return 0;
}
In this example, we have an array arr of size 5 containing integers. We create a pointer ptr and assign it
the address of the first element of the array using arr. Then, we use pointer arithmetic to access each
element of the array and print their values.
Here's an example:
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);
4
return 0;
}
In this example, we have a function printArray that takes a pointer to an integer and the size of the array
as parameters. Inside the function, we iterate over the array using the pointer and print its elements. In
the main function, we call printArray and pass the arr array along with its size.
You can use pointers to arrays in file handling operations such as reading from or writing to
files.
Here's an example of reading an array of integers from a file using a pointer:
#include <iostream>
#include <fstream>
int main() {
std::ifstream inputFile("input.txt");
if (!inputFile) {
std::cerr << "Failed to open the file." << std::endl;
return 1;
}
int arr[5];
5
inputFile.close();
return 0;
}
In this example, we have a file named "input.txt" that contains five integers. We open the file using
ifstream and check if it was successfully opened. Then, we declare an array arr of size 5. Using pointer
arithmetic, we read the integers from the file into the array. Finally, we close the file and print the
elements of the array.
6
Lab Tasks
Lab Task 1:
Write a program that uses a pointer to an array. Declare an array of integers and initialize
it with some values. Then, declare a pointer that points to the first element of the array. Use
pointer arithmetic to iterate over the array and print its elements using the constant
pointer.
Lab Task 2:
Create a function that takes a constant pointer as a parameter. Inside the function, modify the
value being pointed to by the constant pointer. Write a program that calls this function and
passes a pointer to an integer variable as an argument. Print the value of the integer before and
after calling the function to observe the effect of modifying the value through a constant pointer.
Lab Task 3:
Write a function that takes an array of integers and its size as parameters. The function should
find the maximum element in the array using a pointer and return it.
Lab Task 4:
Implement a program that reads a sequence of floating-point numbers from a file named
"data.txt" and calculates their average. Use a pointer to iterate over the array and perform the
necessary calculations.
Lab Task 5:
Write a function reverseArray that takes an array of integers and its size as parameters. The
function should reverse the order of elements in the array using pointers. Test the function by
reversing an array and printing the reversed array.
Lab Task 6:
Implement a program that reads a sentence from the user and counts the occurrence of each
character in the sentence. Use a pointer to iterate over the sentence and a dynamic array to store
the count of each character. Print the count of each character at the end.
7
Lab Task 7:
Write a C++ program to find sum, average of three numbers by taking input from file into the
variables and allocate the memory dynamically to those variables and send the two pointers to
the function where sum will be calculated. You can use required functions.
Lab Task 8:
Write a C++ program to define a pointer variable. Then assign the address of a variable ‘num’ to
the pointer and then you will have two ways to access the value of num; one way is direct using
num variable and other one is by using pointer. Use the pointer variable to manipulate the value
of num and then store the answers in file of name ‘results.txt’. User can add, multiply, divide and
subtract any number from ‘num’.
Lab Task 9:
Create three parallel arrays of same size in order to store ids, working years and salaries of
employees working in an XYZ organization. Take employee’s data from user and store it in arrays.
Now your task is to remove those employees which are present on indices that are multiple of 3.
Display the remaining data of employees at the end of your program.