[go: up one dir, main page]

0% found this document useful (0 votes)
481 views6 pages

Lab Manual 10

The document discusses pointers in C++. It includes examples of declaring and using pointers to integers and arrays, pointer arithmetic, comparing pointers, passing pointers to functions, and returning multiple values from a function using pointers. Key topics covered include declaring pointer variables, storing and dereferencing addresses, incrementing pointers to traverse arrays, comparing values at pointer addresses, and using call by reference to return multiple values from a function.

Uploaded by

Videos4u iK
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)
481 views6 pages

Lab Manual 10

The document discusses pointers in C++. It includes examples of declaring and using pointers to integers and arrays, pointer arithmetic, comparing pointers, passing pointers to functions, and returning multiple values from a function using pointers. Key topics covered include declaring pointer variables, storing and dereferencing addresses, incrementing pointers to traverse arrays, comparing values at pointer addresses, and using call by reference to return multiple values from a function.

Uploaded by

Videos4u iK
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/ 6

Lab Manual # 10 Pointers

10.1 Introduction to Pointers


int *myptr ;
myptr is pointer to an integer

10.1.1 Example of Pointers


#include<iostream.h>
void 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;
}

10.2 Pointer To Arrays


#include<iostream.h>
void main ()
{
int array[5] = {31,54,77,52,93};
for(int j =0; j<5; j++)
{
cout<<array[j]<<endl;

}
}
#include<iostream.h>
void main ()
{
int array[5] = {31,54,77,52,93};
int* ptr;
ptr = array;
for(int j =0; j<5; j++)
{
cout<<*(ptr++)<<endl;
}
}
10.2 Que No 1 Print the values from array
Write a program that prints the values from an array using pointer variable. The array
is given below
int y [ 10 ]= {6,2,3,12};
Code

10.3 Que No 2 Print the values and memory address from an array
Write a program that prints the values from an array using pointer variable. The array
is given below
int y [ 10 ]= {6,2,3,12};

Code

10.4 Pointer Arithmetic

int x =10 ;
int *yptr ;
yptr = &x ;
*yptr += 3 ;
yptr += 3 ;

10.4.1 Example of Pointer arithmetic

long* pnumber = NULL;


long number1 = 10, number2 = 20;

pnumber = &number1;
*pnumber += 2;

cout<<"\nnumber1 = "<<number1
<<" &number = "<<pnumber;
pnumber = &number2;
number1 = *pnumber *4;

cout<<"\nnumber1 = "<<number1 <<"


pnumber = "<<pnumber
<<"pnumber = "<<*pnumber;

10.4.2 Output

number1 = 12 &number =
0x0012FF78 number1 = 80 pnumber =
0x0012FF74 pnumber = 20
Press any key to continue

10.5 Que No 3 Accessing values by Arithmetic operator


Write a program that displays the values using pointer variable from an array
given below using Arithmetic Increment operator .

int y[5]={22,33,44,55,66};

10.5.1 Code
10.6 Que No 4 Moving in array through pointers
th
Write a program that display only 6 element of an array given below using pointers.

int y [10] ={11,22,33, 44,55,66,77,88,99,110}

10.6.1 Code

10.7 Pointer Comparison

if ( y1 > y2 )
if ( y1 >= y2 )
if ( y1 == y2 )

if ( *y1 > *y2 )


10.7.1 Pointer Comparison Example

int y [10]={11,22,33,44,55,66,77,88,99,110} ;

int *y1, *y2;


y1= &y[0];
y2= &y[3];;

cout <<"\n Y1= "<<*y1;


cout <<"\n Y2= "<<*y2;

if (*y1 < *y2)


cout<<"\nY1 is Smaller"<<endl;
else
cout<<"\nY2 is smaller"<<endl;

10.9 Pointer to functions


main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
cout<<”\na =”<<a<<” b= ”<<b;
}
swapv ( int x, int y )
{
int t ; t
=x;x
=y;y
=t;
cout<<”\nx = ”<<x<<” y = ”<<y;
}

The above given code is swapping the values without pointers.

10.9.1 Que No 5 Swap the same values using pointers.


10.10 Que No 6 Returning more than one values from a function
Write a program that gets the radius from user, pass radius to a function areaperi() and
function areaperi() returns ―area‖ and ―perimeter‖ by reference

Using a call by reference intelligently we can make a function return more than
one value at a time, which is not possible ordinarily.

10.10.1 Code

You might also like