Lab 13 Pointers

advertisement
ISE-103 Fundamentals of Computer Programming
Pointers
Program # 01: Write a program to assign two values to two
integer variable a & b. Assign the memory addresses of variables
a & b to pointer variables x & y respectively. Print out the
memory addresses of variables a & b through their pointer
variables also print values of a & b through their pointers.
Solution:
#include<iostream.h>
#include<conio.h>
void main (void)
{
clrscr();
int a, b;
//Pointer Declaration
int *x, *y;
a = 126;
b = 19;
x = &a;
y = &b;
cout<<"Memory address of variable a = "<<x<<endl;
cout<<"Memory address of variable b = "<<y<<endl;
cout<<"Value in Memory address x = "<<*x<<endl;
cout<<"Value in Memory address y = "<<*y<<endl;
getch();
}
Program # 02: Write a program to assign a value to a
variable using its pointer variable. Print out the
value using the variable name and also print out the
memory address of the variable using the pointer
variable.
Solution:
#include<iostream.h>
#include<conio.h>
void main (void)
{
clrscr();
int *p;
int a;
p = &a;
cout<<"Enter data value for a = ";
cin>>*p;
cout<<"Value of variable a = "<<*p<<endl;
cout<<"Memory address of variable = "<<p<<endl;
getch();
}
Program # 03: Write a program to input data into an
array and then to print on the computer screen by using
pointer notation.
Solution:
#include<iostream.h>
#include<conio.h>
void main (void)
{
clrscr();
int array[5], *pp;
pp = array;
cout<<"Plz enter five integers:"<<endl;
for(int i = 0; i<=4; i++)
cin>>array[i];
cout<<endl<<"Values from array:"<<endl;
for(int j = 0; j<=4; j++)
cout<<*pp++<<endl;
getch();
}
Program # 04: Write a program to input data into an
array and find out the maximum value from array through
pointer.
Solution:
#include<iostream.h>
#include<conio.h>
void main (void)
{
clrscr();
int array [5], *pp, max;
pp=array;
cout<<"Enter five values into array:"<<endl;
for(int i = 0; i<=4; i++)
cin>>array[i];
max = *pp;
for(int j = 0; j<=4; j++)
{
*pp++;
if(max<*pp)
max=*pp;
}
cout<<"Maximum value is = "<<max<<endl;
getch();
}
Program # 05: Passing Pointer as Arguments to Function
#include<iostream.h>
#include<conio.h>
void main (void)
{
clrscr();
void Temp(int *, int *);
int a, b, *x, *y;
x = &a;
y = &b;
a = 10;
b = 20;
Temp(x,y);
cout<<"Value of a after passing pointer as argument =
"<<a<<endl;
cout<<"Value of b after passing pointer as argument =
"<<b<<endl;
getch();
}
void Temp(int *x, int *y)
{
*x = *x + 100;
*y = *y + 100;
}
Program # 06: Write a program to swap two values by
passing pointers as arguments to function.
Program # 07: Write a program to convert Fahrenheit
temperature to Celsius degrees by passing pointers as
arguments to the function.
Program # 08: Write a program to convert kilogram into
grams by passing pointers as arguments to function.
Program # 09: Write a program to find out the length of
a string by using pointers.
Solution:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main (void)
{
clrscr();
int LengthOfString(char *);
char st [25], *pp;
pp = st;
cout<<"Enter any string:\t";
gets(st);
cout<<"Length of String is = "<<LengthOfString(pp);
getch();
}
int LengthOfString(char *st)
{
int length = 0;
while(*st!='\0')
{
length++;
*st++;
}
return length;
}
Program # 10: Write a program to copy one string to
another string by using pointers.
Download