Saturday, October 30, 2010

Pointers in C++:Pointers and array:program to reverse a string using pointers


Pointers in C++

Pointers in C++
Pointers in C++ are important concepts as pointers deal with memory addresses. If used wisely, pointers can enhance the efficiency of a program manifold but if used carelessly the program can run to crash. So the programmer should be very careful while using pointers in C++.
What is a pointer?
A pointer is a variable which contains memory address.
For example:
int var=10;
int *ptr; // a pointer of integer type named ptr is declared. See the syntax.
ptr=& var // ptr now holds the address of var using ‘&’ operator. ‘&’ operator is ‘address of’ operator
65524 is the address of var variable. ptr holds this address. 75678 is the address of ptr variable.
Remember a pointer can contain address only for that data type variable which is the type of the pointer itself.
For example:
int var1=2;
float var2=23.54;
int *ptr;
ptr=&var2; //Wrong! ptr is of int type and var2 is of float type
ptr=&var1; //Right. ptr is of int type and var1 is also int type
‘&’ and ’*’ operators
‘&’ is ‘address of’ operator which takes the address of its operand as we have seen in the above examples.
‘*’ operator is ‘value at address’ operator. ‘*’ operator takes any address as its operand and gives the value at that address. ‘*’ is also called ‘indirection operator’.
For example:
int var=10;
int *ptr; // a pointer of integer type named ptr is declared. See the syntax.
ptr=& var; // ptr now holds the address of var using ‘&’ operator. ‘&’ operator is ‘address of’ operator
65524 is the address of var variable. ptr holds this address. 75678 is the address of ptr variable.
Now see the following statements:
cout<<var; //prints 10
cout<<&var; // prints 65524
cout<<ptr; // prints 65524
cout<<&ptr; // prints 75678
cout<<*(&var); // prints 10
cout<<*ptr; // prints 10
In the last two statements ‘*’ operator is used to print value.
Pointer Arithmetic :
Only addition and subtraction is possible in pointers. The calculation is done as following:
int var=10;
int *ptr;
ptr=& var;
From the previous diagram, 65524 is the address of var variable. ptr holds this address. 75678 is the address of ptr variable.
If we write
ptr =ptr+3;
it means add 3 times the the size of integer because ptr is of integer type.
So ptr will contain 65524+3x2
ptr will contain 65530.
If ptr is a float pointer and has address 65545
And we write
ptr= ptr-5;
it means subtract 5 times the size of float because ptr is of float type
ptr=65545-5x4
ptr=65525
Pointers and Arrays
 An array itself is a pointer which holds the address of its first cell. In an array all the elements are stored in contiguous memory locations.
Look at the following program

array and pointer

#include<iostream.h>
void main()
{
int  arr[5]={23,45,65,44,89};
 cout<<arr<<"\n";
 cout<<arr+2<<"\n";
 cout<<*arr<<"\n";
 cout<<*(arr+2)<<"\n";
}  



In the above program an integer array named arr is created and initialized with 5 integer numbers.
The statements will give the output as:
cout<<arr<<"\n";             will print 65524
 cout<<arr+2<<"\n";       will print 65528
 cout<<*arr<<"\n";         will print 23
 cout<<*(arr+2)<<"\n"; will print 65
Now let us see another example.
Following is a program to accept a string and print the string in reverse using pointer. It also counts the length of the string.

pointer and string

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 char str[10];
 cout<<"\nEnter A String\n";
 cin>>str;
 char *ptr;
 int len=0;
 for(ptr=str;*ptr!='\0';ptr++,len++);
 cout<<"String Length Is: "<<len<<"\n";
 cout<<"\Reverse String: ";
 for(--ptr;ptr>=str;ptr--)
 cout<<*ptr;
 getch();
}

OUTPUT:
Enter A String
hello
String Length Is: 5
Reverse String: olleh
Program Explanation:
First Loop: ptr is initialized with str which gives the address of first cell of str. ptr is forwarded by the statement ptr++. The loop runs till ptr does not find ‘\0’. In an array the memory locations are contiguous so ptr forwards to the next cell. A variable len is used to store the length of the string. Each time the loop runs, len is increased by 1. This is an empty loop. When the loop terminates ptr holds the address of the cell containing ‘\0’. len contains the string length.
Second Loop: ptr is initialized with the address of the last character. It runs till the first character of the string. It is moved backwards. Each time loop runs the characters are printed in the reverse order on the screen.
So we have seen that pointers in C++ are very useful. Pointers in C++ are also used for dynamic memory allocation.
“new” operator is used for dynamic memory allocation. See the syntax.
int *ptr = new int;
and this ptr can be released from memory using delete operator. see the syntax.
delete ptr;




No comments:

Post a Comment