Pointers in C++

A variable is a memory location which can store a value and each memory location has its own address.
Memory addresses are represented by long hexadecimal numbers.

To access the address of any variable we need to use & operator. Such as:
 int x = 10;  
 cout<<x; // this will output 10  
 cout<<endl; // next line  
 cout<<&x; // this will output the address of x  


Pointer is a storage entity that can hold the address of any variable. You need to specify the type of variable whose value pointer will hold, at the time of declaration.


You can declare a pointer as :

 int * p1;  
 char * p2;  
 string * x;   

p1 is pointer for integer variable

p2 is pointer for char variable
x is pointer for string variable
Similarly, you can create a pointer for any valid data type in C++.

To use a pointer, you need to assign it the address of the variable that it will point to. You can access the value stored in the variable that p points to by using the * operator. 



 int x = 10;  
 int * po;  
 p = &x; // now p points to x  
 cout<<p; // address of x  
 cout<<*p; // 10 or value of x  


Whenever you declare a pointer, you should initialize it to point to NULL. It essentially means that the pointer points to nothing. Mostly, NULL is defined to 0.

 int * p = NULL;  

You can also put a conditional check on a pointer. When we write if(pointer), the if block will get executed only if the pointer is not pointing to NULL.


++, --, + and - operations can also be performed on pointers. 

 int * p = NULL;  
 int arr[] = { 10, 20, 30, 40 };  
 p = arr; // since arr is itself a pointer  
 cout<<*p; // is value 10  
 cout<<(p+1) // address of p incremented by 4 bytes [size of 32 bit int]   
 cout<<*(p+1) // is value 20  
 cout<<*(p+2) // is value 30  
 cout<<*(p+3) // is value 40  

Pointer is incremented or decremented by the size of data type of the variable it points to.

Pointers can be also compared by using >, < and ==

You can pointers in this manner to access all elements of an array. Remember, you can not modify the constant array name pointer. Therefore, it is invalid to do arr++ in the above example. However you can do *arr = 100, this is valid.

Since p is not a constant pointer, p++ and *p = 100 both are valid.

An array of pointers is declared as , in this case it is pointers to integers :

 int * p[3];  

This will create three integer pointers at p[0], p[1] and p[2]

Similarly, for char pointers :
  char *states[4] = { "NY", "NC", "CL", "New Jersey" };  

states[0] --> *states --> NY

states[1] --> *(states+1) --> NC
states[2] --> *(states+2) --> CL
states[3] --> *(states+3) --> New Jersey

Example :

 char *s[] = {"Dex","abc"};  
 cout<<*s<<" "<<s<<endl;       // Dex 0x7ffeeb294b30  
 cout<<*(s+1)<<" "<<(s+1)<<endl; // abc 0x7ffeeb294b38  
 cout<<s[0]<<endl; // Dex  
 cout<<s[1]<<endl; // abc  

A pointer to pointer is a pointer that points to a pointer. It stores the address of another pointer and is declared as:

 int **pp;  

Example: 

 int **pp = NULL;  
 int *p = NULL;  
 int x = 10;  
 p = &x;  
 pp = &p;  
 cout<<"**pp "<<**pp<<endl; // value of x i.e. 10  
 cout<<"*pp " <<*pp<<endl; // value of p i.e. address of x, same as p  
 cout<<"pp "<<pp<<endl; // value of pp i.e address of p, same as &p  

To pass a pointer to a function, you need to specify it in the function declaration and make sure the value you pass to the function is an address or a pointer.

 void myfunction(int * p) { }   
then pass value as
 int x =10;  
 myfunction(&x);  

To return a pointer from a function, you again need to specify it in the function declaration as: 
 int * myfunction() { }  

Remember, to return address of a local variable out a function, you will need to declare the local variable as static.


Whenever we use a pointer to a structure, to access one of its elements then we use -> operator. While, normally we use . operator.

Comments

Popular posts from this blog

In Place Algorithms

References in C++