0% found this document useful (0 votes)
4 views

PF LAB 11

The lab manual focuses on pointer variables in C++, including their declaration, initialization, and usage in function arguments. Students will learn to manipulate data using pointers through various examples and exercises. The manual also includes homework assignments to reinforce the concepts learned in the lab session.

Uploaded by

Ahmad Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PF LAB 11

The lab manual focuses on pointer variables in C++, including their declaration, initialization, and usage in function arguments. Students will learn to manipulate data using pointers through various examples and exercises. The manual also includes homework assignments to reinforce the concepts learned in the lab session.

Uploaded by

Ahmad Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Fundamentals

Lab Manual (Lab 12)

Topic: Pointer variable definition and initialization, Pointer operators, Passing


arguments to functions by reference (CLO 1,2,3)

Course Instructor:
Lab Instructor:

Session: Fall 2024

School of Systems and Technology


Department of Computer Science
UMT Lahore Pakistan
Pointers
Objective:
To learn how to declare and use pointers.
Scope:
The student should know the following at the end of this lab:
1. How to declare pointer variables.
2. How to assign address of memory location to the pointer variable.
3. How to access values pointed by pointers.
Useful Concept
Pointers are symbolic representation of addresses and can be used to access and manipulate data stored
at the address to which it is pointing.

Declaring pointers:
• Just like other variables, pointers have to be declared before their usage.
Syntax:
int x=5;
int *p;
p=&x;

• Data type of variable and pointer must be same like in this case int x and int *p.

• The is called indirection operator.

• The & or address operator, is a unary operator that returns the address of its operand.

How many levels of pointers can you have?


There is no limit.
int i = 0; int
*ip01 = & i;
int **ip02 = &
ip01; int
***ip03 = &
ip02; int
****ip04 = &
ip03; int
*****ip05 = &
ip04; int
******ip06 = &
ip05; int
*******ip07 =
& ip06; int
********ip08 =
& ip07; int
*********ip09
= & ip08;
Examples
Example 1: Showing fundamentals of pointers

#include <iostream>
using namespace std;
int main(){
int* pc;
int c =
22;;
cout<<"Address of c:"<<&c<<endl;
cout<<"Value of c: "<<c<<endl;
pc=&c;
cout<<"Address of pointer pc: "<<pc<<endl;
cout<<"Content of pointer pc: "<<*pc<<endl;
c=11;
cout<<"Address of pointer pc: "<<pc<<endl;
cout<<"Content of pointer pc: "<<*pc<<endl;
*pc=2;
cout<<"Address of c: "<<&c<<endl;
cout<<"Value of c: "<<c<<endl;
return 0;
}
The output of the program is:
Example 2: Addition of two numbers making use of pointers

#include <iostream>
using namespace std;
int main(){
int first, second, *p, *q, sum;
cout<<"Enter two integers to add:";
cin>>first>>second;
p = &first; q =
&second; sum
= *p + *q;
cout<<"Sum of entered numbers: "<<sum<<endl;
return 0;
}

The output of the program is:

Example 3: Passing pointer as an argument.

#include <iostream>
#include <ctime>
using namespace
std;
void getSeconds(unsigned long *par);
int main () {
unsigned long sec;
getSeconds( &sec );

// print the actual value


cout << "Number of seconds :" << sec << endl;
return 0;
}
void getSeconds(unsigned long *par) {
// get the current number of seconds
*par = time( NULL );
return;
}

The output of the program is:

Example 4: Pointers and arrays-I


#include<iostream> using namespace std; int main(){ int
b[] = {10,20,30,40}; int i, offset; int *ptr = b; //setting
ptr to point to the first element of b.
cout<<"Using array subscripted notation"<<endl;
for (i = 0; i<4; i++) cout<<"b ["<<i<<"] ="<<b
[i]<<" "; cout<<"\n Using array Name/offset
Notation"<<endl; for (offset = 0; offset<4;
offset++)
cout<<"*(b+"<<offset<<")="<<*(b+offset)<<" ";
cout<<"\nUsing pointer subscript
notation"<<endl; for (i = 0; i<4; i++)
cout<<"ptr["<<i<<"] ="<<ptr [i]<<" "; cout<<"\n
Using pointer/offset notation"<<endl; for (offset
= 0; offset<4; offset++)
cout<<"*(ptr+"<<offset<<")="<<*(ptr+offset)<<" ";
return 0;
}
The output of the program is:

Example 5: Pointers and arrays-II

#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;

// pass pointer to the array as an argument.


avg = getAverage( balance, 5 ) ; cout <<

"Average value is: " << avg << endl;

return 0;
}
double getAverage(int *arr, int size) {
int i, sum = 0; double avg;
for (i = 0; i < size; ++i) { sum
+= arr[i];
}
avg = double(sum) / size;
return avg;
}

The output of the program is:

Exercises for lab


Exercise 1 Write a program to declare 5 pointers and print their values.
Exercise 2 Write a program to demonstrate the pointers’ arithmetic.
Exercise 3 Write a program to implement basic mathematics (Add, Subtract, Multiply) by using
functions and pointers.
Exercise 4 Write a program to declare a pointer of 1D Array and print its addresses.
Home Work

1. Write a program to sort a 2D array (using pointers).


2. A program to declare a pointer of 2D array and check its address is even or odd (using functions).

You might also like