Definition
A pointer is a variable that
holds a memory address, usually the location of another variable in memory. Pointer
variable hold address of it’s own type variable i.e. an integer pointer will
hold address of integer type variable and a character pointer will hold address
of character type variable.
Use of pointer
1. Pointers provide the means through
which the location of a variable can be directly accessed and hence can be
manipulated in the way as required.
2. Pointers support c++’s dynamic
allocation routines.
Disadvantages
1.
Un-initialized, or wild pointers
can cause system to crash.
2.
It is easy to use pointers
incorrectly, causing bugs that are very difficult to find.
Syntax for pointer declaration
Data_type * variable_name; // * can be on right side
Data_type * variable_name; // * can be on left side
Data_type * variable_name; // * can be on center
Declaration of pointers
int *iptr; // integer pointer
char *cptr; // character
pointer
float *fptr; // float pointer
double *dptr; // double
pointer
long *lptr; // long pointer
Initialization of pointers
int i=25;
ptr=&i;
char ch=’a’;
cptr
=&ch;
float flt =
3.14;
fptr =
&flt;
double dbl =
1.23622;
dptr =
&dbl;
long lg =
123452;
lptr =
≶
The two special operator
(* and
&) are used with pointers. The & is a unary operator that
returns the memory address of its operand. * is the dereference operator. It is
also called as “value at the address of”.
It returns the value present at the
address which is hold by pointer.
Example: Pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int a =10;
int *b = &a;
cout<<"\n value of a "<<a;
cout<<"\n value of a "<<*b;
cout<<"\n address of a
"<<&a;
cout<<"\n address of a
"<<b;
getch();
}
Output
Video
Example: Pointer to Pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int a=10,*b,**c;
b=&a;
c=&b;
cout<<"\n value of a "<<a; //value of a
cout<<"\n value of a "<<*b; //value of a
cout<<"\n value of a "<<**c; //value of a
cout<<"\n value of b "<<*c; //address of a, because value of b is address of a
cout<<"\n value of b "<<b; //address of a, because value of b is address of a
cout<<"\n address of b "<<&b; //address of b
cout<<"\n address of b "<<c; //address of b, c is pointer to pointer variable which hold a pointer variable address that is *b
cout<<"\n address of c "<<&c; //address of c
getch();
}
Output
void main()
{
int a=10,*b,**c;
b=&a;
c=&b;
cout<<"\n value of a "<<a; //value of a
cout<<"\n value of a "<<*b; //value of a
cout<<"\n value of a "<<**c; //value of a
cout<<"\n value of b "<<*c; //address of a, because value of b is address of a
cout<<"\n value of b "<<b; //address of a, because value of b is address of a
cout<<"\n address of b "<<&b; //address of b
cout<<"\n address of b "<<c; //address of b, c is pointer to pointer variable which hold a pointer variable address that is *b
cout<<"\n address of c "<<&c; //address of c
getch();
}
Output
Pointer Arithmetic
Only two arithmetic operation, addition and subtraction may be performed on pointers. When we add 1 to a pointer, we are actually adding the size of whatever the pointer is pointing at, i.e. each time a pointer is incremented by 1. It points to the memory location of the next element of its base type.
Example
int *ptr; //current address is => 1001
ptr++; // new address will be => 1003
ptr--; // new address will be => 1001
Video
Output
#include<conio.h>
void main()
{
int a,b;
int *p,*q;
p=&a;
q=&b;
cout<<"Enter value of a ";
cin>>*p;
cout<<"Enter value of b ";
cin>>*q;
if(*p>*q)
cout<<a<<" is greater ";
else
cout<<b<<" is greater ";
getch();
}
Output
Video
Video
Video
#include<conio.h>
void main()
{
int a[][3]= {
{1,2,3},
{4,5,6},
{7,8,9}
};
int *p = &a[0][0];
cout<<"\n Matrix elements are \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<*(*(a+i)+j)<<" ";//a[i][j]
}
cout<<endl;
}
getch();
}
Video
#include<conio.h>
void main()
{
int (*a)[3];//3 is the number of columns
a=new int[3][3];//assigning memory to element of each row
int i,j;
cout<<"\n Enter Elements of Matrix \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>*(*(a+i)+j); //a[i][j]
}
}
cout<<"\n Matrix Elements are "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<*(*(a+i)+j)<<" ";
}
cout<<"\n";
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
void main()
{
char *string1="Good Morning";
cout<<string1<<endl;
char string2[]="welcome to school";
char *tempstring = string2;
cout<<tempstring<<endl;
char str[]="Hello how are you";
tempstring = &str[0];
cout<<tempstring<<endl;
char *ptr="c++ programming";
while(*ptr!='\0')
{
cout<<*ptr;
++ptr;
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
void main()
{
char string1[]="hello";
char string2[10];
char *ptr1="Good Morning ";
char *ptr2;
//string2=string1;//assignment of one string to another is not possible
ptr2=ptr1; //assignment of one pointer string to another pionter string is possible
cout<<"First string\t:"<<string1<<"\n";
cout<<"Copied String\t:"<<string2<<"\n";//will show blank, because string not copied
cout<<"First Pointer String\t:"<<ptr1<<"\n";
cout<<"Copied Pointer string\t:"<<ptr2<<"\n";
getch();
}
Output
#include<iostream.h>
#include<conio.h>
void main()
{
char *names[] ={"amir","shil","neelam","iram"};
for(int i=0; i<4; i++)
{
cout<<names[i]<<" ";
}
getch();
}
Output
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h> //for strlen() function
void main()
{
char *namesArray[5];
char tempname[15];
char *pointer;
#include<conio.h>
int digitSum(int *num)
{
int remainder, sum=0;
while(*num>0)
{
remainder=*num%10;
sum=sum+remainder;
*num=*num/10;
}
return sum;
}
void main()
{
int number;
cout<<"\n Enter A Multidigit Number ";
cin>>number;
cout<<"\n Number Digit Sum is : "<<digitSum(&number);
getch();
}
Output
Video
1. A dynamically allocated object not deleted using delete.
2. Delete statement is not getting executed because of some logic error.
3. Assigning the result of a new statement to an already occupied pointer.
The Memory Leak Can Be Avoided By
1. Making sure that a dynamically allocated object is deleted.
2. A new statement stores its return value (a pointer) in a fresh pointer.
#include<conio.h>
void main()
{
int *Array,raw,column,i,j;
cout<<"Enter Number of Rows : ";
cin>>raw;
cout<<"Enter Number of Columns : ";
cin>>column;
//Declaration of Dynamic Array
Array=new int[raw*column];
cout<<"\nEnter Elements of Matrix "<<endl;
for(i=0;i<raw;i++)
{
for(j=0;j<column;j++)
{
cin>>Array[i*column+j];
}
}
cout<<"\nMatrix Elements Are "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<Array[i*column+j]<<" ";
}
cout<<endl;
}
getch();
}
ptr--; // new address will be => 1001
A pointer holds the address of
the very first byte of the memory location where it is pointing to. The address
of first byte is known as base address.
Example: Arithmetic operation on pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int i=10;
int *ptr=&i;
cout<<"address of i "<<ptr<<endl;
ptr++;
cout<<"address of location next to i "<<ptr<<endl;
ptr--;
cout<<"address of i again "<<ptr<<endl;
++ptr;
cout<<"address of location next to i "<<ptr<<endl;
--ptr;
cout<<"address of i again "<<ptr<<endl;
getch();
}
Output
Output
Video
Increment And Decrement of Variable Values By Pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int n=44;
int *ptr=&n;
cout<<"\n real value of n "<<*ptr;
++*ptr;
cout<<"\n vlaue of n after pre increment "<<*ptr;
(*ptr)++;
cout<<"\n value of n after post increment "<<*ptr;
--*ptr;
cout<<"\n value of n after pre decrement "<<*ptr;
(*ptr)--;
cout<<"\n value of n after post decrement "<<*ptr;
getch();
}
#include<conio.h>
void main()
{
int n=44;
int *ptr=&n;
cout<<"\n real value of n "<<*ptr;
++*ptr;
cout<<"\n vlaue of n after pre increment "<<*ptr;
(*ptr)++;
cout<<"\n value of n after post increment "<<*ptr;
--*ptr;
cout<<"\n value of n after pre decrement "<<*ptr;
(*ptr)--;
cout<<"\n value of n after post decrement "<<*ptr;
getch();
}
Output
Arithmetic operation (Addition, Subtraction, Division, Multiplication) by pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
int *p,*q;
p=&a;
q=&b;
cout<<"Enter value of a ";
cin>>*p;
cout<<"Enter value of b ";
cin>>*q;
int add=*p+*q;
cout<<"\n Sum is "<<add;
int sub=*p-*q;
cout<<"\n substraction is "<<sub;
int product=*p * *q;
cout<<"\n Product is "<<product;
int div=*p / *q;
cout<<"\n Division is "<<div;
getch();
}
Output
Pointer With If Else
#include<iostream.h>#include<conio.h>
void main()
{
int a,b;
int *p,*q;
p=&a;
q=&b;
cout<<"Enter value of a ";
cin>>*p;
cout<<"Enter value of b ";
cin>>*q;
if(*p>*q)
cout<<a<<" is greater ";
else
cout<<b<<" is greater ";
getch();
}
Output
Video
Reverse Digits of a Multi Digit Number By Pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int num, remainder, sum=0;
int *ptr;
cout<<"Enter a multi digits number ";
cin>>num;
ptr=#
while(*ptr>0)
{
remainder = *ptr%10;
sum = sum*10+remainder;
*ptr=*ptr/10;
}
cout<<"Reverse number is "<<sum;
getch();
}
Output
Pointer With Array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5];
int i,*p;
p=a;
//or
//p=&a[0];
cout<<"\n Enter Elements of Array "<<endl;
for(i=0;i<5;i++)
cin>>*(p+i);
cout<<"\n Array Elements are \n";
for(i=0;i<5;i++)
cout<<*(p+i)<<endl;
getch();
}
Output
#include<conio.h>
void main()
{
int a[5];
int i,*p;
p=a;
//or
//p=&a[0];
cout<<"\n Enter Elements of Array "<<endl;
for(i=0;i<5;i++)
cin>>*(p+i);
cout<<"\n Array Elements are \n";
for(i=0;i<5;i++)
cout<<*(p+i)<<endl;
getch();
}
Output
Video
Bubble Sort By Pointer Array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp,*p;
p=a;
cout<<"\n Enter Elements of Array \n";
for(i=0;i<5;i++)
cin>>*(p+i);
for(i=0;i<5;i++)
{
for(j=0;j<(5-i-1);j++)
{
if(*(p+j)>*(p+(j+1)))
{
temp=*(p+j);
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
cout<<"\n Array After Sorting "<<endl;
for(i=0;i<5;i++)
cout<<*(p+i)<<" ";
#include<conio.h>
void main()
{
int a[5],i,j,temp,*p;
p=a;
cout<<"\n Enter Elements of Array \n";
for(i=0;i<5;i++)
cin>>*(p+i);
for(i=0;i<5;i++)
{
for(j=0;j<(5-i-1);j++)
{
if(*(p+j)>*(p+(j+1)))
{
temp=*(p+j);
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
cout<<"\n Array After Sorting "<<endl;
for(i=0;i<5;i++)
cout<<*(p+i)<<" ";
getch();
}
}
Output
Video
Pointer With Two D Array With Initialization Method
#include<iostream.h>#include<conio.h>
void main()
{
int a[][3]= {
{1,2,3},
{4,5,6},
{7,8,9}
};
int *p = &a[0][0];
cout<<"\n Matrix elements are \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<*(*(a+i)+j)<<" ";//a[i][j]
}
cout<<endl;
}
getch();
}
Output
Video
Pointer With Two D Array By User Input Method
#include<iostream.h>#include<conio.h>
void main()
{
int (*a)[3];//3 is the number of columns
a=new int[3][3];//assigning memory to element of each row
int i,j;
cout<<"\n Enter Elements of Matrix \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>*(*(a+i)+j); //a[i][j]
}
}
cout<<"\n Matrix Elements are "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<*(*(a+i)+j)<<" ";
}
cout<<"\n";
}
getch();
}
Output
Pointer With String
String can be declared with the help of pointer. There various ways through which we can declare string with the help of pointer. String with pointer is declared by char pointer only. All the method to declare a string with pointer we represented in below shown program.
#include<conio.h>
void main()
{
char *string1="Good Morning";
cout<<string1<<endl;
char string2[]="welcome to school";
char *tempstring = string2;
cout<<tempstring<<endl;
char str[]="Hello how are you";
tempstring = &str[0];
cout<<tempstring<<endl;
char *ptr="c++ programming";
while(*ptr!='\0')
{
cout<<*ptr;
++ptr;
}
getch();
}
Output
Assignment of Char Pointer To One Another
Assignment of one string to another is not possible while assignment of one pointer string to another pointer string is possible. We can see it in below example.
#include<iostream.h>
#include<conio.h>
void main()
{
char string1[]="hello";
char string2[10];
char *ptr1="Good Morning ";
char *ptr2;
//string2=string1;//assignment of one string to another is not possible
ptr2=ptr1; //assignment of one pointer string to another pionter string is possible
cout<<"First string\t:"<<string1<<"\n";
cout<<"Copied String\t:"<<string2<<"\n";//will show blank, because string not copied
cout<<"First Pointer String\t:"<<ptr1<<"\n";
cout<<"Copied Pointer string\t:"<<ptr2<<"\n";
getch();
}
Output
String Array By Pointer
We can declare an array of strings by pointer in C++. For this we have to take an array of character pointer (char *).
In below example we declare a character array names (char *names[]). In it we can see that each element is string literal. String literal always points to the base address of the first character. So the base type of each element of the names array is a pointer to char or char *.
#include<conio.h>
void main()
{
char *names[] ={"amir","shil","neelam","iram"};
for(int i=0; i<4; i++)
{
cout<<names[i]<<" ";
}
getch();
}
Output
String Array By Pointer By User Input Method
Neither we can insert directly a STRING into STRING ARRAY nor we can assign a STRING to another STRING.
For this first we have to read a STRING. Than we will assign this STRING to a CHARACTER POINTER. After that this CHARACTER POINTER will assign this STRING to STRING ARRAY.
We can implement this entire process like as shown in below program.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h> //for strlen() function
void main()
{
char *namesArray[5];
char tempname[15];
char *pointer;
for(int i=0; i<5; i++)
{
cout<<"\n Enter a Name ";
//reading single name by user in tempname variable
gets(tempname);
//getting length of string stored in tempname by strlen() function
int length = strlen(tempname);
{
cout<<"\n Enter a Name ";
//reading single name by user in tempname variable
gets(tempname);
//getting length of string stored in tempname by strlen() function
int length = strlen(tempname);
//assigning dynamic memory to pointer by new operator of size length+1
pointer = new char[length+1];
//copying tempname string into pointer string, because string directly can not assign to another string
strcpy(pointer, tempname);
//assigning pointer string to namesArray's ith position character pointer
namesArray[i]=pointer;
}
pointer = new char[length+1];
//copying tempname string into pointer string, because string directly can not assign to another string
strcpy(pointer, tempname);
//assigning pointer string to namesArray's ith position character pointer
namesArray[i]=pointer;
}
cout<<"\n Names You Entered are"<<endl;
for(int i=0; i<5; i++)
{
cout<<names[i]<<endl;
}
getch();
}
for(int i=0; i<5; i++)
{
cout<<names[i]<<endl;
}
getch();
}
Output
Digits Sum of A Multi Digit Number By Pointer To Function
#include<iostream.h>#include<conio.h>
int digitSum(int *num)
{
int remainder, sum=0;
while(*num>0)
{
remainder=*num%10;
sum=sum+remainder;
*num=*num/10;
}
return sum;
}
void main()
{
int number;
cout<<"\n Enter A Multidigit Number ";
cin>>number;
cout<<"\n Number Digit Sum is : "<<digitSum(&number);
getch();
}
Output
Function With Pointer Return Type
#include<iostream.h>
#include<conio.h>
//Function Prototype
int * reverseDigit(int *);
void main()
{
int
number;
cout<<"\nEnter
A Multi Digit Number : ";
cin>>number;
int
*ptr, tempNumber;
tempNumber=number;//Storing a temprary
copy of orignal number
ptr=reverseDigit(&number);//Function
Call
if(*ptr==tempNumber)
{
cout<<"\nNumber Is Palindrome ";
}
else
{
cout<<"\nNumber
Is Not Palidrome";
}
getch();
}
//Function Definition
int * reverseDigit(int *num)
{
int
remainder, sum=0;
while(*num>0)
{
remainder=*num%10;
sum=sum*10+remainder;
*num=*num/10;
}
return
∑
}
Output
Video
Memory Allocation
Each data element or variable is provided some memory; and this process of allocating memory is known as memory allocation. This process is of two types.
- Static Memory Allocation
- Dynamic Memory Allocation
Static Memory Allocation
When the amount of memory to be allocated is known before
handed and the memory is allocated during compilation itself, is known as
static memory allocation.
Ex. int i , In this case the compiler knows that 2 bytes
will be allocated to i, because it is an integer variable.
Dynamic Memory Allocation
The process of allocating memory at the time of execution is
called dynamic memory allocation. The allocation and release for this memory
space can be done with the help of some operators Ex. new, delete. The new operator
allocates memory dynamically and returns a pointer storing the memory address
of the allocated memory. The operator delete free the memory pointed by
a pointer.
Free Store
It is a pool or heap of Un-allocated memory provided to a program so that it can utilize during
execution. The free store memory is without name. Objects allocated on the free
store are manipulated indirectly through pointers.
Another aspect of free store is
that the allocated memory is uninitialized. The free store memory is allocated
through the use of new operator and
de-allocated through delete operator.
Free store memory is
dynamically allocated during run time and static memory allocation takes place
during compile time.
Memory Leaks
If the objects, that are allocated memory dynamically, are not deleted using delete, the memory block remains occupied even at the end of the program. Such memory blocks are known as orphaned memory blocks. This orphaned memory blocks when increase in number, bring an adverse effect on the system. This situation is known as memory leak. The possible reason for this are:
1. A dynamically allocated object not deleted using delete.
2. Delete statement is not getting executed because of some logic error.
3. Assigning the result of a new statement to an already occupied pointer.
The Memory Leak Can Be Avoided By
1. Making sure that a dynamically allocated object is deleted.
2. A new statement stores its return value (a pointer) in a fresh pointer.
Dynamic Single Dimension Array
#include<iostream.h>
#include<conio.h>
void main()
{
int size;//Static
Memory Allocation
cout<<"\nEner
Size of Array : ";
cin>>size;
//Dynamic Memory Allocation
int *Array=new int[size];
cout<<"\nEnter
Array Elements"<<endl;
for(int i=0;i<size;i++)
{
cin>>Array[i];
}
cout<<"\nArray
Elements Are \n\n";
for(int
i=0;i<size;i++)
{
cout<<Array[i]<<"\t";
}
getch();
}
Output
Dynamic Double Dimension (2D) Array
#include<iostream.h>#include<conio.h>
void main()
{
int *Array,raw,column,i,j;
cout<<"Enter Number of Rows : ";
cin>>raw;
cout<<"Enter Number of Columns : ";
cin>>column;
//Declaration of Dynamic Array
Array=new int[raw*column];
cout<<"\nEnter Elements of Matrix "<<endl;
for(i=0;i<raw;i++)
{
for(j=0;j<column;j++)
{
cin>>Array[i*column+j];
}
}
cout<<"\nMatrix Elements Are "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<Array[i*column+j]<<" ";
}
cout<<endl;
}
getch();
}
Output
Comments
Post a Comment