You are on page 1of 5

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

LAB MANUAL 05
CSC 2207 Programming Language 2 [EEE]
TITLE

A Brief Introduction to Pointers

PREREQUISITE

• To know about variables


• To be able to use cin and cout
• To know about arrays
• To know about functions
• To know about loop control structure
• To know about decision control structure

OBJECTIVE

• To get a basic idea about pointers


• To gain experience with pointer declaration, initialization and access
• To understand about pointer in array and function
• To know about pointers and strings

THEORY

Pointer

In C++, pointers are variables that store the memory addresses of other variables.

A pointer is a variable that stores the location of memory. In more fundamental terms, a
pointer stores the address of a variable. In more picturesque terms, a pointer points to a
variable. A pointer has to be declared just like any other variable. For example: int *p; is a
pointer to an integer. Adding an asterisk (called the de-referencing operator) in front of a
variable's name declares it to be a pointer to the declared type.
#include <iostream>
using namespace std;

int main()
{
int x=10;
int *p;
p=&x; // p hold address of x ; initialization
cout<<x; // 10
cout<<&x; // print address of x
cout<<p; // print p hold the address
cout<<&p; // address of p
cout<<*p; // 10 ; dereferencing of pointer p

return 0;
}

int A[]={1,4,3,5,2}; // array created in main program memory(Stack memory)


// like any other variables.
int *p = new int[5]; // p created in main memory but array
//created in Heap memory (Because of new operator) and p points to Array

Pointer (Call by Address)

#include <iostream >


using namespace std;

int Swap(int *a,int *b){


int temp;
temp = *a;
*a= *b;
*b = temp;
}

int main() {
int x = 10;
int y = 20;
Swap(&x,&y);
cout<<x<<" "<<y;

return 0;
}
Pointer (Call by Reference)

#include <iostream >


using namespace std;

int Swap(int &a,int &b){


int temp;
temp = a;
a= b;
b = temp;
}

int main() {

int x = 10;
int y = 20;
Swap(x,y);
cout<<x<<" "<<y;

return 0;
}

Function and pointer

#include <iostream >


using namespace std;

int maximun(int x,int y){


return x>y?x:y;
}

int minimum(int x, int y){


return x<y?x:y;
}

int main() {
int (*fp)(int, int);
fp = maximun;
cout<<(*fp)(10,5)<<endl;
fp = minimum;
cout<<(*fp)(10,5)<<endl;
return 0;
}
Array and pointer

#include <iostream>
using namespace std;

void display(int *m ,int length) {


for (int i = 0; i < 5; ++i) {
//pointer dereference and array operation
// are same
cout << *(m+i) <<" "<<m[i] <<endl;
}
}

int main() {
// declare and initialize an array
int marks[5] = {88, 76, 90, 61, 69};
display(marks,5);
return 0;
}

LAB WORK

1. Take Inputs from User and Store them in an Array and pass that array to Display function
as arguments, a Sum and Average function to find sum and average of Array Elements.
USE Function, array and pointer.
2. Declare a pointer to a function that accepts three pointers to integer quantities as
arguments and returns a pointer to sum of all quantity.

ASSIGNMENT

1. Write a program using pointers to read in an array of integers and print its elements in
reverse order.
2. Write a function using pointers to add two matrices and to return the resultant matrix to
the calling function.
3. Using pointers, write a function that receives a character string and a character as
argument and deletes all occurrences of this character in the string. The function should
return the corrected string with no holes.

You might also like