Professional Documents
Culture Documents
Class: BEE-13D
Fall 2022
Lab04: Pointers
Name:Abdurrahim
Reg no:369247
Lab Objective:
To have better understanding of pointers
Lab Description:
Pointers are powerful features of C++ that differentiates it from other programming languages like
Java and Python. Pointers are used in C++ program to access the memory and manipulate the
address.
Address in C++
To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's memory.
The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The &(reference)operator gives
you the address occupied by a variable.
Activity 01:
In the following example, you may not get the same result on your system. The 0x in the
beginning represents the address is in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs from third
by 4-bytes. This is because the size of integer (variable of type int) is 4 bytes in 64-bit
system.
Code OUTPUT
The statement above defines a pointer variable p. It holds the memory address.
The asterisk is a dereference operator which means pointer to. Here, pointer p is a pointer to
int, i.e., it is pointing to an integer value in the memory address.
For example: If a number variable is stored in the memory address 0x123, and it contains a
value 5.
The reference (&) operator gives value 0x123, while the dereference (*)
operator gives the value 5.
The (*) sign used in the declaration of C++ pointer is not the dereference pointer. It is just a
similar notation that creates a pointer.
The pointer variable “p” can hold the memory address of variable of any data type.
int c, *pc;
2. pc=c; /* Wrong! pc is address whereas, c is not an address.*/
Call by Reference
You learned about passing arguments to a function. This method used is called passing by value
because the actual value is passed.
However, there is another way of passing an argument to a function, where the actual value of the
argument is not passed. Instead, only the reference to that value is passed.
Think??
T1. Point out error(s) if any in the following programs.
Code:
#include<iostream>
using namespace std;
//--------------------------------------------------------------------------------
void swap(int&x, int & , int &); // decleration protype
void print(int a, int b, int c) {
cout << " the numbers of element 1" << ":";
cout << a << endl;
cout << " the numbers of element 2" << ":";
cout << b << endl;
cout << " the numbers of element 3" << ":";
cout << c << endl;
}
//-------------------------------------------------------------------------------------------
int main() {
int a,b,c;
swap(a,b,c);
print(a, b, c);
return 0;
}
//--------------------------------------------------------------------------
void swap(int &x, int &y, int &z) { // decleration protype
int temp1, temp2;
temp1 = x;
temp2 = y;
x = z;
y = temp1;
z = temp2;
}
Output:
Code:
#include<iostream>
using namespace std;
//--------------------------------------------------------------------------------
int main() {
int array[5]; // declaring type to array
for (int i = 0; i < 5; i++) {
cout << "ENter the number " << i + 1 << ":";
cin >> *(array + i); // userdefined values by pointers
}
cout << endl << endl; // for space
for (int i = 0; i < 5; i++) {
cout << "the number " << i + 1 << ":";
cout << *(array + i)<<endl;
CS 212: Object Oriented Page 9
Programming
}
}
Output:
CODE:
#include<iostream>
#include<cstdlib> // for srand and rand
#include<time.h>// for time
using namespace std;
//--------------------------------------------------------------------------------
void populate_array(int* array, int N);
// this initializes an array of size N with random values.
int main() {
int a[10], x;
populate_array(a,10);
print_array(a, 10);
sort(a, 10);
cout << endl << "after sorting" << endl;
print_array(a, 10);
cout << endl << endl << "eneter the number:";
cin >> x;
if ((find(a, x, 10)))
cout << "it is present";
else
cout << "it is not present";
}
void populate_array(int* array, int N)
{
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++) {
*(array + i) = rand()%20;
}
}
///////////////////////////////////////////////////////////////
void print_array(int* array,int N)
{
for (int i = 0; i < N; i++) {
cout << "the value of output is " << i + 1 << ":"<< *(array + i)<<endl;
Your main should just have the function calls, input for finding a number and one statement for showing if
number exists in the array or not.
Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS in a single word file.
CS 212: Object Oriented Page
Programming 14