C++ Pointers

Pointer is variable which are store the address of another variable. pointer is very interesting topic because they are utilize the address of memory location. Using of & (address of operator) compiler are providing the memory location of variable which are occupied in RAM section. and help of * (Dereferencing operator) utilize the memory of those location like changing the value and get the value so on.

Declaration pointer

Pointer variable are declared by using of a data type. followed by *(asterisk) symbol and name of variable. look at view an syntax to declare pointer variables.

data_type * variable_name;

Key points Description
data_type predefined and user defined data type like int float struct and so on
* (asterisk) Dereferencing operator
variable_name Name of variable
For example
int age; //normal variable
int *ptr;// integer pointer variable

Here declaring are two integers variable. one is capable to store the value and another one is capable to store of integer variable address. how to read this pointer variable this is question are arising in your mind?. there are not given any specific rule but most of cases we read pointer variable from right to left. see an example.

//ptr is a pointer variable to integers
int *ptr;
  
/*count is a pointer variable 
    to a constant integers*/
const int *count;

look at simple example to implement pointer variables.

//example of pointer variable in c++
#include<iostream>
using namespace std;

int main(){

  int age=90;
  int *ptr;
  //assign address of ptr using & operator
  ptr=&age; //assign address
  //displaying the relations of variables
  cout<<"age  :"<<age<<endl;
  cout<<"ptr  :"<<ptr<<endl;
  cout<<"address of age  :"<<&age<<endl;
  cout<<"address of ptr  :"<<&ptr<<endl;
  cout<<"address of age value :"<< *(&age)<<endl;
  cout<<"address at value of ptr :"<<*ptr<<endl;
  cout<<"address of ptr value :"<<*(&ptr);    
  return 0;
}
Output
age  :90
ptr  :0x61ff0c
address of age  :0x61ff0c
address of ptr  :0x61ff08
address of age value :90
address at value of ptr :90
address of ptr value :0x61ff0c

In this example understand the following points.

1) Address of operator (&) : This is provide the address of variable.

2) De-referencing operator (*) : Help of this we are get and set the value of location.

This two is core concept of points. If you understand this point so very easy to learn pointers. view another example.

//example of pointer variable in c++
#include<iostream>
using namespace std;

int main(){
  char status='T';//T true assume

  /*Declare an char pointer 
   And assign the address of another char variable.
   This is done by single statement*/
  char *auxiliary=&status;
  
  cout<<"status inital value : "<<status<<endl;
  cout<<"*auxiliary inital value :"<<*auxiliary<<endl;

  //modified pointer value at address
  *auxiliary='F';//F flase

  cout<<"status after value : "<<status<<endl;
  cout<<"*auxiliary after value :"<<*auxiliary<<endl;

  return 0;
}
Output
status inital value : T
*auxiliary inital value :T
status after value : F
*auxiliary after value :F

Note that in this program are interesting facts. auxiliary is char pointer that are store the address of status char variable. and using of (*) operator we can modified the value of status variable. that is the power of pointer. this are deal with variable address.

beginners are common mistake in pointers

1) Trying to access uninitialized value of pointer. see example.

// common mistake of pointer in c++
#include<iostream>
using namespace std;

int main(){
  int x;
  int *ptr;
  cout<<*ptr;//access uninitialized address value
  return 0;
}
Output : printed garbage value.

In this program not assigned an address to pointer variable. so this are prints garbage value. note that avoid this situation because when pointer variable are used to perform any arithmetic operation that are print unwanted result or some case they are crash our program.

2) assigning address of variable. but variable value are not assigned. see this example.

// example 2 common mistake of pointer in c++
#include<iostream>
using namespace std;

int main(){
  int x;
  int *ptr=&x;//assign address
  cout<<*ptr;//access garbage value
  return 0;
}
Output : printed garbage value.

In this case x integer variable are not assign any value and help of pointer try to access value of x memory location. this is are similar problem in previous example. and try to avoid of this situations.

3) how to assign address of pointer variable. view the situations.

// example 3 common mistake of pointer in c++
#include<iostream>
using namespace std;

int main(){
  int counter=10,index=0;
  int *ptr1=&counter;
  int *ptr2;
  //logical mistake
  *ptr2=&index; //error of this line
  return 0;
}
Error
error: invalid conversion from 'int*' to 'int' [-fpermissive]
  *ptr2=&index; //error of this line
        ^~~~~~

In this case compilation error. observe that *ptr2 are initial no value (address). this are contain by default garbage value. and previous *ptr1 are declare and assignment are single statement. so that are valid for complier. so how to solve this problem. see this program.

// how to use pointer variable example in c++
#include<iostream>
using namespace std;

int main(){
  int counter=10,index=0;
  int *ptr1=&counter;
  int *ptr2;
  //remove asterisk
  ptr2=&index; 
  cout<<*ptr2;
  return 0;
}
Output
0

so best practice before use of pointer provide valid memory address of variable. if pointer are initial not address then it will assign NULL macro. NULL is contain zero value. look at an example.

// NULL macros in pointer variable
#include<iostream>
using namespace std;

int main(){
  //assign NULL macros
  int *ptr=NULL;
  int data=100;
  if(ptr!=NULL){
    cout<<"ptr Not NULL";
  }else{
      ptr=&data;//assign address
      cout<<*ptr;
  }
  return 0;
}
Output
100

Pointer to Pointer

Pointer which are store the address of another pointer. this process is similar to multiple indirection. view the declaration of multiple pointer.

int *single_ptr;
int **double_ptr;
int ***triple_ptr;
//:
//:

There are possible to include many number of (*) before of pointer variable. but note that this process are change the behavior of pointer. In technical word this process are provide multiple indirection of pointer. see example.

//multiple levels indirection example in pointer
#include<iostream>
using namespace std;

int main(){
  
  int data=10;
  int *ptr1=NULL;
  int **ptr2=NULL;
  int ***ptr3=NULL;
  int ****ptr4=NULL;
  //assign address 
  ptr1=&data;
  ptr2=&ptr1;
  ptr3=&ptr2;
  ptr4=&ptr3;
  //display pointer at values
  cout<<"data :"<<data<<endl;
  cout<<"*ptr1 :"<<*ptr1<<endl;
  cout<<"**ptr2 :"<<**ptr2<<endl;
  cout<<"***ptr3 :"<<***ptr3<<endl;
  cout<<"****ptr4 :"<<****ptr4<<endl;
  return 0;
}
Output
data :10
*ptr1 :10
**ptr2 :10
***ptr3 :10
****ptr4 :10

Pointer Behaviours

1) Size of pointer : size of pointer is based on compiler. 16 bit compiler are allocate 2 bytes of memory. and 32 bit compiler are allocate 4 bytes. see the example.

// size of pointer in c++
#include<iostream>
using namespace std;

int main(){

  int *int_ptr=NULL;
  float *float_ptr=NULL;
  char *char_ptr=NULL;
  void *void_ptr=NULL;

  //display size
  cout<<" int_ptr :"<<sizeof(int_ptr)<<endl;
  cout<<" float_ptr :"<<sizeof(float_ptr)<<endl;
  cout<<" char_ptr :"<<sizeof(char_ptr)<<endl;
  cout<<" void_ptr :"<<sizeof(void_ptr)<<endl;
  return 0;
}
Output
 int_ptr :4
 float_ptr :4
 char_ptr :4
 void_ptr :4

Note that 32 bit compiler is providing this result.

2) void pointer : void pointer is special type of pointer. they are deal with all predefined and user defined data type. they are reliable to accept any data type address. look at view an example.

// void pointer example in c++
#include<iostream>
using namespace std;

int main(){

  int int_data=10;
  float float_data=60.39f;
  double double_data=50.34;
  char char_data='A';
  
  //assign intger variable address
  void *ptr;
  ptr=&int_data;
  cout<<*(int*)ptr<<endl; //display 10

  //assign float variable address
  ptr=&float_data;
  cout<<*(float*)ptr<<endl; //display 60,39

  //assign double variable address
  ptr=&double_data;
  cout<<*(double*)ptr<<endl; //display 50.34

  //assign char variable address
  ptr=&char_data;
  cout<<*(char*)ptr<<endl; //display A
  return 0;
}
Output
10
60.39
50.34
A

Note that before access void pointer variable first need to it that typecasting. because few gcc compiler are provide compilation error.

3) global and static pointer: global and static pointer variable default value is NULL. static variable are create on one time. see the example

// void pointer example in c++
#include<iostream>
using namespace std;
//global pointer
int *auxiliary;

void test(int value){
  //static pointer variable
  static int*ptr;
   if(ptr==NULL){
    cout<<"Static pointer by default NULL"<<endl;
  }
  ptr=&value;
  cout<<*ptr<<endl;
}
int main(){
  int status=1;

  if(auxiliary==NULL){
    cout<<"Global pointer by default NULL"<<endl;
  }

  auxiliary=&status;
  cout<<*auxiliary<<endl;
  test(11);
  test(123);
  return 0;
}
Output
Global pointer by default NULL
1
Static pointer by default NULL
11
123

note that there are two time call test function. initial static variable are NULL so it will display message. but next time this are not statement inside (if condition). that is behaviour of static variables.

4) Constant pointer variable

// const pointer example in c++
#include<iostream>
using namespace std;

int main(){
  int value=786;
  //assign address of value integer
  const int *ptr=&value;
  //access value
  cout<<*ptr<<endl;
  int temp=100;

  //assign address of temp integer
  ptr=&temp;
   //access value
  cout<<*ptr<<endl;
  return 0;
}
Output
786
100

First time every programmer are not accepting this result. because constant means not change any value of variable but in this program are change the address and also provide output when run this program. but this is valid statement. so what happening here?.

const int *ptr;

ptr is a pointer to constant integers. that means ptr are change itself but cannot change at address of values. see example.

// const pointer example in c++
#include<iostream>
using namespace std;

int main(){
  int value=786;
  //assign address of value integer
  const int *ptr=&value;
  *ptr=22; //try to change value
  //access value
  cout<<*ptr<<endl;
  return 0;
}
Error
error: assignment of read-only location '* ptr'
  *ptr=22; //try to change value

5) Constant pointers to an constants : This are restricted and avoid the value and address of pointer variable there will define as following syntax.

 const int  * const variable_name;

For example view this program.

#include<iostream>
using namespace std;
int main(){

  int foo=11;
  //ptr is a constant pointer to an integer constant
  const int * const ptr=&foo;
  cout<<*ptr;
  return 0;
}
Output
11

In this case there are not possible to assign another address of (ptr) pointer variable. see the example.

#include<iostream>
using namespace std;
int main(){

  int foo=11;
  //ptr is a constant pointer to an integer constant
  const int * const ptr=&foo;
  cout<<*ptr;
  int bar=15;
  *ptr=&bar; //error in this line
  return 0;
}
Error
error: assignment of read-only location '*(const int*)ptr'
  *ptr=&bar; //error in this line

© 2019, regularcodes.com, All rights reserved