Professional Documents
Culture Documents
What is pointer?
o A pointer is a variable which holds the memory address of another variable.
o If one variable contains the address of another variable then the first variable is said as
“point to the second.”
Advantages of pointer
o It supports dynamic allocation and deallocation of memory segment.
o It allows to establish links between data elements or objects for some complex data
structure such as link list, stack etc.
o A pointer allows to return structure variables from structure.
o A pointer improves the efficiency of certain routines.
o No extra space required for formal variables.
o It provides function which can modify their calling argument.
o It allows to pass variables, arrays, strings and structure as argument.
Pointer Declaration
o A pointer is a variable which holds the memory address of another variable.
o Syntax:
o datatype * pointer_variable;
o e.g. int *ptr;
o where ptr is a pointer variable which holds the address of an integer data type.
o A Pointer variable consists of two parts
Pointer Operator
Address Operator
Pointer Operator
o It can be represented by combination of *(asterisk) with a variable name.
o The asterisk(*) must be preceded by the pointer variable.
o Syntax:
Datatype * pointer_variable;
Here, datatype may be int, float, char etc.
e.g.
int *ptr;
where, ptr is a pointer variable which holds the address of an integer data type.
Address Operator
o It can be represented by combination of &(ampersand) with a pointer variable.
o The ampersand (&) is the unary operator that returns the memory address of its operand.
o Syntax
datatype variable;
datatype *pointer_variable = &variable;
e.g.
int a;
int *ptr=&a; //ptr recieves the address of a variable
*(ptr++) Retrieve the content of location pointed by ptr and then increment ptr.
*(++ptr) Increment ptr and then retrive the content of location pointed by ptr.
(*ptr)++ Retrive the content of location pointed by ptr and then increment the
content of location.
++(*ptr) increment the content of location and then retrive the content of
location pointed by ptr.
Example
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"After swap"<<endl;
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
}
Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]
void main()
{
clrscr();
int a,b;
cout<<"Enter value of a & b"<<endl;
cin>>a>>b;
cout<<"Before swap"<<endl;
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
swap(a,b);
getch();
}
Call by Reference
In this method address of the variable is passed to a function.
When a function is called by portion of program the address of actual arguments are copied
on to formal arguments, through they may be referred by different variable name.
Formal and actual variables have same memory space only accessed by different name or
link.
Changes made to the actual parameters will affect to the actual parameter.
i. In this method value of the variable is i. In this method address of the variable is
passed to a function. passed to a function.
ptr=&a[0];
cout<<"Enter The size of array";
cin>>n;
cout<<"Enter the data elements"<<endl;
for(i=0;i<n;i++)
{
cin>>*ptr;
ptr++;
}
ptr=&a[0];
cout<<"Array element="<<endl;
for(i=0;i<n;i++)
{
cout<<*ptr;
ptr++;
}
getch();
}
Pointer to String
Calculating String Length
#include<iostream.h>
#include<conio.h>
void main()
{
char str[10],*ptr;
int len=0;
cout<<"Enter the string ";
cin>>str;
ptr=str; //ptr=&str[0]
while(*ptr!='\0')
{
len++;
ptr++;
}
cout<<"Length <<len;
getch();
}
void main()
{
char str[10],*ptr;
int len=0,count=0;
cout<<"Enter the string ";
cin>>str;
ptr=str; //ptr=&str[0]
while(*ptr!='\0')
{
if(*ptr=='a'||*ptr=='e'||*ptr=='i'||*ptr=='o'||*ptr=='u')
{
count++;
}
ptr++;
}
cout<<"Vowels count="<<count;
getch();
}
String Copy
#include<iostream.h>
#include<conio.h>
void main()
{
char str1[10],str2[10],*ptr1,*ptr2;
ptr1=str1; //ptr1=&str1[0]
ptr2=str2; //ptr2=&ptr2[0]
while(*ptr1!='\0')
{
*ptr2=*ptr1;
ptr1++;
ptr2++;
}
*ptr2='\0';
while(*ptr2!='\0')
{
*ptr1=*ptr2;
ptr1++;
ptr2++;
}
*ptr1='\0';
cout<<"Concatinated string= "<<str1;
getch();
}
String Reverse
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],*ptr1,*ptr2;
int len=0;
clrscr();
cout<<"Enter the string ";
cin>>str1;
ptr1=str1; //ptr=&str[0]
ptr2=str2;
while(*ptr1!='\0')
{
len++;
ptr1++;
}
ptr1--;
while(len!=0)
{
*ptr2=*ptr1;
ptr2++;
ptr1--;
len--;
#include<iostream.h>
#include<conio.h>
#include<string.h>
class xyz
{
int rn;
char name[10];
public:
void accept()
{
cout<<"Enter roll no and name";
cin>>rn>>name;
}
void display()
{
cout<<"Roll No="<<rn<<endl;
cout<<"Name="<<name<<endl;
}
};
void main()
{
xyz x1;
xyz *ptr;
clrscr();
ptr=&x1;
ptr->accept();
ptr->display();
getch();
}
#include<iostream.h>
#include<conio.h>
class xyz
{
int rn;
char name[10];
public:
void accept()
{
cout<<"Enter roll no and name";
cin>>rn>>name;
}
void display()
{
cout<<"Roll No="<<rn<<endl;
cout<<"Name="<<name<<endl;
}
};
void main()
{
xyz x1[2];
xyz *ptr;
clrscr();
ptr=x1; //&x1[0];
for(int i=0;i<2;i++)
{
ptr->accept();
ptr->display();
ptr++;
}
getch();
}
x1.display();
cout<<"Object Addrs x1="<<&x1<<endl;
x2.display();
cout<<"Object Addrs x2="<<&x2<<endl;
getch();
}