You are on page 1of 6

LAB #03 Pointers

Void Pointers:
Use Case:
It is a general-purpose pointer which can store variables of different datatypes in a similar
address.

Given Scenario:
You are given with a task to save a student’s information in the same memory
address i-e (Name, Roll No, Section) to make the program more lightweight.
How would you implement it? How would you access each field from the same
address?
Coding Implementation:
#include<iostream>
#include <string>
using namespace std;

int main()
{
void* info;
string name = "Mohib";
int RollNo = 107;
char Section = 'C';
bool FeeStatus = true;

info = &name;
cout <<"Name: "<< *(string*)info<<endl;
// The Value From Void Pointer Is Accessed By Using Typecasting
info = &RollNo;
cout <<"Roll No: "<< *(int*)info<<endl;
info = &Section;
cout << "Section: " << *(char*)info << endl;
info = &FeeStatus;
cout << "Fee Status: " << *(bool*)info << endl;
cout << (info)<<endl;

cout << "Address Of Each Data: " << (info) << endl;
}

Name: Mohib Uddin


OUTPUT:

NULL Pointers:
Use Case:
• Passing Pointers To Function As An Argument Without A Valid Memory
Address Without Crashing.
Given Scenario:
• Create A User Defined Function Which Takes A Pointer And An Array As
Argument.Using The Pointer Find The Greatest Value From The Array
And Return The Value Of The Pointer.
Coding Implementation:
#include<iostream>
#include <string>
using namespace std;

int FindMax(int *ptr, int arr[5])


{
ptr = &arr[0];
for (int i = 0; i < 5; i++)
{
if (arr[i] > *ptr)
{
ptr = &arr[i];
}
}
return *ptr;
}
int main()
{
int* ptr=NULL;
int Arr[5] = { 10,20,55,21,24 };
int MaxValue=FindMax(ptr,Arr);
cout <<"The Maximum Value From The Array:"<< MaxValue;
}

Name: Mohib Uddin


Output:

Dangling Pointers:
Use Case:
A pointer pointing to a memory location that has been deleted (or freed) is
called dangling pointer. There are three different ways where Pointer acts as
dangling pointer.
Scenario 1: Function Call:
#include<iostream>
#include <string>
using namespace std;

int* getHello(void)
{
int n = 75;
return &n;
}

int main()
{
cout << getHello();
}
OUTPUT:

Scenario 2: Dynamic Memory Allocation:


#include<iostream>
#include <string>
using namespace std;

int main()
{
int* p = new int; // request memory
*p = 5; // store value

Name: Mohib Uddin


delete p; // free up the memory
// now p is a dangling pointer
cout << p;
}
OUTPUT:

Pointer To An Array:
Given Scenario:
Write a C++ program called “Eliminate odds”. First, create an array named “values” having
10 elements of integer type. Then, the program should ask the user to enter values (which
may be negative as well as positive) in this array. After that, the program replaces each
array element which is odd with an even value. Positive odds should be replaced by even
values by adding 1 to them while the negative odd values must be replaced by adding -1 to
them. In the end, the program shows the updated array. Please Note: Array input, output,
and other accesses must be done using pointer notation only (no subscript [ ] notation
allowed).

Coding Implementation:
#include<iostream>
#include <string>
using namespace std;

int main()
{
int values[10] = { 1,3,5,-9,8,15,-13,-17,29,20 };
int* ptr;
ptr= values;
cout << "Original Array:" << endl;
for (int i = 0; i < 10; i++)
{
cout << " " << *(ptr + i);
}
cout << endl;
//Eliminating Odd Numbers In The Array By Above Conditions
for (int i = 0; i < 10; i++)
{
if (*(ptr + i) % 2 != 0)
{

Name: Mohib Uddin


if (*(ptr + i) >= 0)
{
*(ptr + i)=*(ptr+i)+1;
}
if (*(ptr + i) < 0)
{
*(ptr + i) = *(ptr + i) - 1;

}
}
}

cout << "Odd Numbers In The Array Eliminated:" << endl;


for (int i = 0; i < 10; i++)
{
cout << " " << *(ptr + i);
}
}

Time Complexity → O(n)


Output Snippets:

Pointer To Pointer:
Use Case:
The first pointer is used to store the address of the variable. And the second
pointer is used to store the address of the first pointer.
Given Scenario:
Define An Array Of Marks For Two Students. Find The Average Of Marks And
Save Them To Pointer *avg1 and *avg2 Now Compare the two pointers and
store the greater value to a pointer to pointer **top.
Code:
#include<iostream>
#include <string>
using namespace std;

Name: Mohib Uddin


int main()
{
int Std1[5] = { 80,70,65,92,56 };
int Std2[5] = { 90,80,90,92,50 };
int max1=0, max2=0;//Variables To Store Average Values

int* avg1, * avg2, ** topper; //Pointers To Point The Values


for (int i = 0; i < 5; i++)
{
max1 += Std1[i];
max2 += Std2[i];
}
max1 /= 5;
max2 /= 5;
avg1 = &max1;
avg2 = &max2;
if (*avg1 > *avg2)
{
topper = &avg1;
}
else
{
topper = &avg2;
}
cout << "Average Of First Student: " << *avg1 << endl;
cout << "Average Of Second Student: " << *avg2 << endl;
cout << "The Topper: " << **topper << endl;

Output:

Name: Mohib Uddin

You might also like