Pointers in C++





As you know every variable is a memory location and every memory location has its address defined, this address can be accessed using ampersand (&) operator which denotes the address of memory.

Below Example will show how to print the address of variable:-

example:

#include<iostream>
using namespace std;
 int main () {
  int a ,b;
  cout << "Address of a = ";
  cout << &a<< endl;
  cout << "Address of b = ";
  cout << &b<< endl;
  return 0;
}                           
OUTPUT:
Address of a = 0x23fe4c
Address of b = 0x23fe48
                            

Pointer's Definition

A pointer is a variable whose value is the address of another variable ie. direct address of the memory location.

Like any other variable/constant we can declare pointers.

Syntax

data_type * variable_name ;

Example:

int     *p;  //pointer to integer
float   *pt;  //pointer to float
double  *pt;  //pointer to double 
char  *ptr;  //pointer to character

Reference operator (&) and Deference operator (*)

Reference operator (&) as discussed above gives the address of a variable.

To get the value stored in the memory address, we use the dereference operator (*).

int a=10;
int *p;
p = &a; assigning the address
*****************************
int *ptr =&a; initializing the pointer
Now to access the address of variable 'a' we can write use either &a or p.
To access the value of variable 'a' we can write either simple a or *p;

Example:

#include<iostream>
using namespace std;
int main() {
  int *p, a;
  a = 5;
  cout<< "Address of a (&a):";
  cout<< &a << endl;
  cout << "Value of a (a):";
  cout<< a << endl;
  p = &a;
  cout<< "Value of p:";
  cout<< p << endl;
  cout << "Value of a (*p):";
  cout<< *p << endl;
  *p = 20; 
  cout<< "Value of a (a):";
  cout<< a << endl;
  cout<< "Value of a (*p):";
  cout<< a << endl;
  return 0;
}
OUTPUT:
Address of a (&a):0xfe1342
Value of a (a):5
Value of p:0xfe1342
Value of a (*p):5
Value of a (a):20
Value of a (*p):20


Types of Pointer

1. NULL pointer

  • It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned.
  • This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
  • The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program:

Example:

#include<iostream>
using namespace std;
int main() {
 int *p = NULL;
 cout<< "The Value of p is "<< p; 
 return 0;
}
OUTPUT:
The Value of p is 0

2. Generic pointer(void)

  • When a variable is declared as being a pointer to type void it is known as a generic pointer.
  • Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence it is called Generic pointer.
  • This is very useful when you want a pointer to point to data of different types at different times.

Example:

#include<iostream>
using namespace std;
int main() {
  int i;
  char c;
  void *p;  //generic pointer
  i = 10;
  c = 'A';
  p = &i;   //point to integer

  cout<< "pointer points to the integer value ";
  cout<< *(int*) p<< endl;

  p = &c;  //point to character

  cout<<"Now points to the character ";
  cout<< *(char*) p<< endl;
  return 0;
}                        
OUTPUT:
pointer points to the integer value 10
Now points to the character A

void pointers cannot be dereferenced. It can however be done using typecasting the void pointer.
Pointer arithmetic is not possible on pointers of void due to lack of concrete value and thus size.



3. Constant Pointers

  • A constant pointer is a pointer that cannot change the address its holding.
  • In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

Syntax

data_type *const variable_name ;
for e.g:
int *const p;

Its best practice to initialize the constant pointer, because some compiler like GNU/G++ compiler show compile time error if don't initialize the constant pointers.

Example:

#include<iostream>
using namespace std;
int main() {
  int a=5 , b=10;
  int *const p = &a;
  p = &b;  //error
  return 0;
}

4. Pointer to Constant

  • A pointer through which one cannot change the value of variable it points is known as a pointer to constant.
  • These type of pointers can change the address they point to but cannot change the value kept at those address.

Syntax

const data_type *variable_name ;
for e.g:
const int * p;

Example:

#include<iostream>
using namespace std;
int main() {
  int a=5 , b=10;
  const int *p = &a;
  *p = 20;  //error
   p = &b; //valid
  return 0;
}

5. Constant Pointer to Constant

  • Its a mixture of the above two types of pointers.
  • A constant pointer to constant is a pointer that can neither change the address its pointing to and nor it can change the value kept at that address.

Syntax

const type *const variable_name;
for e.g:
const int *const p;

Example:

#include<iostream>
using namespace std;
int main() {
  int a=5 , b=10;
  const int *const p = &a;
  *p = 20;  //error
   p = &b; //error
  return 0;
}


6. Wild pointer or Bad Pointer

  • A Pointer in C++ that has not been initialized till its first use is known as Wild pointer.
  • A wild pointer points to some random memory location.

Example:

#include<iostream>
using namespace std;
int main() {
 int i = 10;
 int *p; //not initialized ,wild pointer
 return 0;
}

7. Dangling pointer

  • If a pointer is pointing to the memory address of a variable but that variable has been deleted or de-allocated, without modifying the value of the pointer, while pointer is still pointing such memory location, Such pointer is known as dangling pointer.
  • In short pointer pointing to non-existing memory location is called dangling pointer.

There are three different situation where Pointer acts as dangling pointer.

1. De-allocation of memory

#include<iostream>
using namespace std;
int main() {
 int *ptr = (int *)malloc(sizeof(int));
 
 // After below free call, ptr becomes a 
 // dangling pointer
   free(ptr); 
     
 // No more a dangling pointer
  ptr = NULL;
return 0;
}

2. Function Call

#include<iostream>
using namespace std;
int *fun()
{
  // x is local variable and goes out of
  // scope after an execution of fun() is
  // over.
  int x = 10;
 
  return &x;
}
int main() {
 int *p = fun();
  fflush(stdin);
 
  // p points to something which is not
  // valid anymore
  cout<< *p;
  return 0;
}
Output :
garbage value

The above problem doesn’t appear (or p doesn’t become dangling) if x is a static variable.

3. Variable goes out of scope

int main()
{
   int *ptr;
   .....
   .....
   {
       int ch;
       ptr = &ch;
   } 
   .....   
   // Here ptr is dangling pointer
} 

Next topic is Array in C++





 











Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved