Pointers in c++

A pointer is a variable that that contains the address of another variable in memory.
 
Declaration of pointer: A pointer variable (ptr)  is declared with asteriks( * )
data_type  *pointer_variable;
example:  int *ptr;
 
Initialization of pointer:
int *ptr =&i;      // initialization during declaration
or
int *ptr;             // declaration
ptr =&i;           //initialization of pointer with address of other variable
 
For clear understanding:
int *ptr;        ——-
|       |    = ptr variable
____
3243int i=5;
——-
|  5    |       // i holds 5
____
2050ptr=&i;
——-
| 2050 |       //ptr holds address of i
____
3243

*ptr=10;
——-
|   10   |
____
2050                // *ptr is variable i,
*ptr  is  *(&i)   which was 5 before ,now overwrites with 10

 
 
 
A pointer  is a variable that pointing to memory address of other variable.
 
*ptr = value at address to which pointer points (ex: 10)
ptr = address of variable to which pointer variable points( ex:  123O2424X45)
 
Example of Pointer:
#include<iostream>
using namespace std;
int main(void)
{
int *ptr,  i=5;                                    //declaration of pointer variable ( ptr) and initialization of  ‘ i ‘    
    // we can declare pointer variable and normal variable together if they have same data type.
ptr=&i;                                             //pointer variable points to address of  variable ‘ i ‘
*ptr=10;                                          // 10 gets stored in address to which the pointer variable points.    i.e  i=10
cout<<“ptr”<<ptr<<“n”;            //it will display memory address of variable ‘ i ‘
cout<<“*ptr”<<*ptr<<“n”;        // it will display value at variable  ‘ i ‘    i.e 10
}
 
There are two types of operators used with pointers:
1)  Refrencing Operator   (&)
2) Derefrencing Operator (*)
 
Refrencing Operator : It is also kown as Address operator. It is used to fetch the memory address of variable.
Derefrencing operator : It is also known as Indirection Operator. It is used to Indirectly access the value of variable.
 
Program to show use of refrencing and de-refrencing operator.
#include<iostream>
using namespace std;
int main(void)
{
int *ptr;
int i=5;
ptr=&i;                      // & is used to fetch(refrence) the address of variable ‘ i ‘
*ptr=10;                    //* is used to indirectly (de-refrence) access the value of ‘i’ .
cout<<“ptr”<<ptr<<“n”;                 // memory address of ‘ i’
cout<<“*ptr”<<*ptr<<“n”;             //value of i         //10
cout<<” *(&i) “<< *( &i)<<“n”;     //to fetch value at address of i                // 10
}
 
 
*(&variable)   means fetching value of variable.
Hence, the pointer variable ptr contains the address of  variable  ‘i ‘
and therefore,
*ptr  returns value of stored in address of other variable ‘ i ‘
*ptr = *(&i) 
 
 
NOTE:
Pointer must be initialized before its use
i.e
ptr = &i       //initialize with address of ‘ i ‘
then,
*ptr = 10   // use 
 
if  Uninitialised variables will not cause any compile time error , but if used will give unappropriate result.

#include<iostream>
using namespace std;
int main()
{
int *a;
int *b;
int *c;
cout<<a<<“n”;
cout<<b<<“n”;
cout<<c<<“n”;
}
 this will not give compile time error ,but unappropriate result.
 
 
Pointer to Pointer:
Pointer to Pointer variable, stores the address of pointer variable( that further stores the address of another variable).
 
#include<iostream>
using namespace std;
int main(void)
{
int **ptr, i, *j;
i=5;
j=&i;
ptr=&j;
cout<<“ptr = “<<ptr<<“t”<<“&j = “<<&j<<“n”;              // ptr = &j
cout<<“*ptr = “<<*ptr<<“t”<<“&i = “<<&i<<“n”;        //*ptr = &i
cout<<“**ptr = “<<**ptr<<“t”<<“i = “<<i<<“n”;        //**ptr = i
}
 
 
 
   int (*ptr)[ 10 ] ;

Which is a pointer to an array of 10 elements. Thus, when you do pointer arithmetic, it can compute the size of the array and handle it correctly. The parentheses are NOT optional above. Without the parentheses, ptr becomes an array of 10 pointers, not a pointer to an array of 10 ints.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s