You are on page 1of 11

Chapter 05 Pointer

 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

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


 Pointer Arithmetic
o Arithmetic operators used in pointers such as
 Addition(+)
 Subtraction(-)
 increment(+ +)
 decrement(- -)
o Pointers are variables.
o They are not integer but they can be display as unsigned integers.
o ptr++; pointer incremented but not by 1.
o ptr-- ; pointer decremented but not by 1.

Arithemtic pointer Description

ptr++ ptr is incremented after execution of statement.

++ptr ptr is incremented before execution of statement.

*(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.

 Pointers and function


o Call by value
o Call by reference
 Call by value
o In this method value of the variable is passed to a function
o Whenever a portion of program invokes a function with formal arguments, control
will be transferred from main to calling function and value of actual arguments are
copied to function.
o Formal and actual variables have different memory space.
o Changes made to formal parameters will not affect to actual parameters.

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.

e.g. void swap(int *a, int *b)


Function calling swap(&a , &b); //address of a and b passed as argument
 Example
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
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);
cout<<"After swap"<<endl;
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
getch();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


Call by value Call by Reference

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.

ii. Whenever a portion of program invokes a


ii. When a function is called by portion of
function with formal arguments, control will
program the address of actual arguments are
be transferred from main to calling function
copied on to formal arguments, through they
and value of actual arguments are copied to
may be referred by different variable name.
function.
iii. Changes made to formal parameters will iii. Changes made to the formal parameters
not affect to actual parameters. will affect to the actual parameter.
iv. It requires extra memory space for formal
iv. It saves the memory space.
parameters.
e.g. void swap(int a, int b) e.g. void swap(int *a, int *b)
{ {
} }

 Pointer To integer -Accept and Display of array using


#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],*ptr,i,n;

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();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


 Searching a value in a given array.
#include<iostream.h>
void main()
{
clrscr();
int a[10],find,f=0,i,n;
int *ptr;
ptr=&a[0];
cout<<"Enter size of array";
cin>>n;
cout<<"Enter value of array element"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter value to search"<<endl;
cin>>find;
for(i=0;i<n;i++)
{
if(*ptr==find)
{
cout<<"No is found";
f=1;
break;
}
ptr++;
}
if(f==0)
{
cout<<"No not found";
}
getch(); }

 Finding Largest Element


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n,max;
int *ptr;

cout<<"Enter size of array";


cin>>n;
cout<<"Enter value of array element"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
max=a[0];
ptr=&a[1];
for(i=1;i<n;i++)
Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]
{
if(*ptr>max)
{
max= *ptr;
}
ptr++;
}
cout<<"Largest element is "<<max<<endl;
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();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


Count Number of Vowels in String
#include<iostream.h>
#include<conio.h>

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;

cout<<"Enter the string ";


cin>>str1;

ptr1=str1; //ptr1=&str1[0]
ptr2=str2; //ptr2=&ptr2[0]

while(*ptr1!='\0')
{
*ptr2=*ptr1;
ptr1++;
ptr2++;
}
*ptr2='\0';

cout<<"Copied string= "<<str2;


getch();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


String Concatenation
#include<iostream.h>
#include<conio.h>
void main()
{
char str1[10],str2[10],*ptr1,*ptr2;
cout<<"Enter the string1 ";
cin>>str1;
cout<<"Enter the string2 ";
cin>>str2;
ptr1=str1; //ptr1=&str1[0]
ptr2=str2; //ptr2=&ptr2[0]
while(*ptr1!='\0')
{
ptr1++;
}

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--;

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


}
*ptr2='\0';
cout<<"Reverse String="<<str2;
getch();
}
Pointer to Object
 A pointer can point to an object created by class.
 Object pointers are useful to create objects at runtime.
 We can use pointer object to access member of class.
 Syntax:
Class_name *ptr_obj;
E.g. xyz *ptr;
ptr= &x1; xyz is class and ptr is the pointer to object x1.

#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();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


Pointer to Array of Object

#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();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]


This Pointer
 It is used to represent an object that invokes a member of
function.
 It is automatically passed to any member function when it is
called.
 Using this pointer, any member function can find out the address
of the object.
 Example
#include<iostream.h>
#include<conio.h>
#include<string.h>
class xyz
{
int rn;
float per;
public:
xyz(int roll,float per1)
{
this->rn=roll;
this->per=per1;
}
void display()
{
cout<<"Roll No="<<this->rn<<endl;
cout<<"Per="<<this->per<<endl;
cout<<"\nAddress of this="<<this<<endl;
}
};
void main()
{
xyz x1(11,60);
xyz x2(22,50);
clrscr();

x1.display();
cout<<"Object Addrs x1="<<&x1<<endl;
x2.display();
cout<<"Object Addrs x2="<<&x2<<endl;

getch();
}

Mr. G. R. Shinde [ Mb-9404984366 mail-grsnsk@gmail.com]

You might also like