Professional Documents
Culture Documents
(CL1004)
LABORATORY MANUAL
Fall 2023
LAB 04
Pointer Basics & Pointers Arrays
Engr. Arslan Ahmed
______________________________________
LAB ENGINEER SIGNATURE & DATE
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;
}
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 .
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
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.
C++ allows us to allocate the memory of a variable or an array in run time. This is known as
We can allocate and then deallocate memory dynamically using the new and delete operators
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;
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;
return 0;
}
double getAverage(int *arr, int size)
{
int i, sum = 0;
double avg;
return avg;
}
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.
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.