Pointer
Pointer
points address of another variable
syntex
datatype *variablename=value;
eg.
int *a=5;
int
*b=&a;
Example:
int a;
int *ptr;
a=10;
ptr=&a;
100 - address of a 200 - address of ptr
value of a value of ptr
a ptr
-a refers to 10
-&a refers to 100
-*(&a) refers to 10
-ptr refers to 100
-&ptr refers to 100
-*ptr refers to 10
program:1
#include<iostream.h>
#include<conio.h>
void main()
{
int i,*iptr;
iptr=&i;
i=20;
clrscr();
cout<<" i value is "<<i<<endl;
*iptr=*iptr+30;
cout<<"i value is "<<i<<endl;
getch();
}
output:
i value is 20
i value is 50
program:2
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[5]={10,20,30,40,50};
int *ptr,i;
ptr=arr;
clrscr();
for(i=0;i<5;i++)
{
cout<<*(ptr+i)<<"\t";
*(ptr+i)=*(ptr+i)+5;
}
cout<<endl;
for(i=0;i<5;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
output:
10 20 30 40 50
15 25 35 45 55
program:3
#include<iostream.h>
#include<conio.h>
void main()
{
char name[10];
char *cptr;
clrscr();
cout<<"enter the name in lower case:";
cin>>name;
cptr=name;
for(;*cptr!='\0';cptr++)
{
if(*cptr>='a'
&& *cptr<='z')
*cptr=*cptr-32;
}
cout<<" upper case is : "<<cptr<<endl;
}
output:
Enter the name in lower case : apple
Upper case is : APPLE
0 comments:
Post a Comment