Professional Documents
Culture Documents
LAB MANUAL 05
CSC 2207 Programming Language 2 [EEE]
TITLE
PREREQUISITE
OBJECTIVE
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 main() {
int x = 10;
int y = 20;
Swap(&x,&y);
cout<<x<<" "<<y;
return 0;
}
Pointer (Call by Reference)
int main() {
int x = 10;
int y = 20;
Swap(x,y);
cout<<x<<" "<<y;
return 0;
}
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;
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.