0% found this document useful (0 votes)
10 views7 pages

New

This document is a lab manual focused on pointers in C++. It includes multiple examples and programs demonstrating pointer usage, pointer arithmetic, and functions that return multiple values using pointers. Key programs include printing array values, finding the maximum of two numbers, swapping values, and calculating the area and perimeter of a circle.

Uploaded by

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

New

This document is a lab manual focused on pointers in C++. It includes multiple examples and programs demonstrating pointer usage, pointer arithmetic, and functions that return multiple values using pointers. Key programs include printing array values, finding the maximum of two numbers, swapping values, and calculating the area and perimeter of a circle.

Uploaded by

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

Lab Manual # 10

Pointers

Example 1:
#include<iostream>
Using namespace std;

Int main ()

Int var1 = 10;


Int var2 = 20;
Int var3 = 30;

Cout<<&var1<<endl<<&var2<<endl;
Int *ptr;

Ptr = &var1;
Cout<<ptr<<endl;
Ptr = &var2;
Cout<<ptr<<endl;

}
Output:

Example 2:
#include<iostream>
Using namespace std;
Int main ()
{

Int array[5] = {31,54,77,52,93};


Int* ptr;
Ptr = array;
For(int j =0; j<5; j++)
{
Cout<<*(ptr++)<<endl;
}

}
Output:

Program No 1: Print the values from array


#include <iostream>
Using namespace std;

Int main(){

Int y [10]= {6,2,3,12};

Int *ptr1=y;
For(int i=0; i<4; i++){
Cout<<*ptr1<<” “;
Ptr1++;
}
}
Output:

Program No 2: Print the values and Addresses from array


#include <iostream>
Using namespace std;

Int main(){

Int y [10]= {6,2,3,12};

Int *ptr1=y;

For(int i=0; i<4; i++){


Cout<<*ptr1<<” “<<endl;
Cout<<ptr1<<” “<<endl;
Ptr1++;
}
}
Output:

Example of Pointer arithmetic

#include <iostream>
Using namespace std;

Int main(){
Long* pnumber = NULL;
Long number1 = 10;
Long number2 = 20;

Pnumber = &number1;
*pnumber += 2;

Cout<<”\nnumber1 = “<<number1
<<” &number = “<<pnumber;

Pnumber = &number2; number1 =


*pnumber *4;

Cout<<”\nnumber1 = “<<number1
<<” pnumber = “<<pnumber
<<”pnumber = “<<*pnumber;
}

Output:

Program No 3: Write a program that display only 6th element of an array


given below using pointers

#include <iostream>
Using namespace std;

Int main(){
Int x [10] ={11,22,33,

44,55,66,77,88,99,110};

Int *ptr_1=&x[5];
Cout<<*ptr_1;
}

Output:

Program No 4: Write a program that take two numbers an input from user.
Find the maximum from both of them using the dereference pointer
comparison

#include <iostream>
Using namespace std;
Int main(){
Cout<<”Enter Two integer type numbers and see which one is
Maximum:\n”;

Cout<<”\nEnter First number\n”;


Int num_1;
Cin>>num_1;
Cout<<”\nEnetr Second number\n”;
Int num_2;
Cin>>num_2;
Int *ptr_1= &num_1;
Int *ptr_2= &num_2;
If( *ptr_1 > *ptr_2){
Cout<<”The Max Number is = “<<*ptr_1<<endl;
}
Else{
Cout<<”The Max Number is = “<<*ptr_2<<endl;
} }
Output:

Program No 5: Swap the values using pointers


#include <iostream>
Using namespace std;
Int main(){

Cout<<”Enter Two Numbers for swaping:\n”;


Cout<<”\nEnter First Number:\n”;
Int num1;
Cin>>num1;
Cout<<”Enter Second Number:\n”;
Int num2;
Cin>>num2;
Int num3;
Int *ptr1= &num1;
Int *ptr2= &num2;
Int *ptr3= &num3;
*ptr3=*ptr1;
*ptr1= *ptr2;
*ptr2= *ptr3;
Cout<<”The Numbers after Swaping are:\n”;
Cout<<” First Number = “<<*ptr1<<endl;
Cout<<” Second Number = “<<*ptr2<<end; }
Output:

Program No 6: Returning more than one values from a function


#include <iostream>
Using namespace std;
Void areaperi(float radius, float &area, float &perimeter);
Int main() {
Float radius, area, perimeter;
Cout << “Enter the radius of the circle:\n”;
Cin >> radius;
Areaperi(radius, area, perimeter);
Cout << “\nArea of a circle = “ << area << endl;
Cout << “\nPerimeter of a circle = “ << perimeter << endl;
}
Void areaperi(float radius, float &area, float &perimeter) {

Area = 3.14159 * radius * radius;


Perimeter = 2 * 3.14159 * radius;
}
Output::

You might also like