C++ Pointers

By | March 15, 2020

Pointer in C++ is one of the most confusing and important topics and it plays a vital role in dynamic memory allocation, for a beginner understanding the concept of pointer could be a daunting task, here we have tried to break the concept for you in simple words, so you could have a basic idea what are pointers in C++ and why do we use them.

Ampersand Operator (&)

Before jumping to pointers let’s discuss the ampersand (&) sign, and you must have encountered this special character in C++ operators where we have used 2 ampersand signs to represent the AND logic operator. But here the & represent memory address, we all know data get stored in memory and we use variable name or Identifies to refer the data value, but when the compiler compiles the program it store data at random free memory location and each memory location has a memory address and to get that memory address we use the single ampersand sign (&).

Vamware

NOTE

 && = AND LOGIC Operator

& = Ampersand Operator to tell the memory address of the object.

Example

#include <iostream>
usign namespace std;  

int main()
   {             
       int variable_1 = 10;
       int  variable_2[12]={12,23,45};               
       cout<<"The Address of variable_1 is: "<<&variable_1;
       cout<<endl;
       cout<<"The Address of first value of variable_1 is: "<<&variable_2[0];
       cout<<endl;
       cout<<"The Address of second value of variable_2 is: "<<&variable_2[1];
   }

Output

The Address of variable_1 is: 0x6ffe0c
The Address of first value of variable_1 is: 0x6ffdd0
The Address of second value of variable_2 is: 0x6ffdd4

Behind the Code

Here in this example, you can see that the address of variable_2[1] is 0x6ffdd4 which is 4 bytes ahead of the address of variable_1[0] with this we can estimate that the array store values at consecutive memory locations.   

Pointer in C++ (*)

A pointer is a variable that is used to store the address of another variable, in simple words a pointer variable store memory address as a value. Like other variables we also have to define the data type and name of the pointer along with the asterisk(*) character as a prefix.

Syntax to define a pointer

Data_type *variable_name;

Example

int *var;

The data type of the pointer variable should be similar to the variable data type which addresses it is supposed to store.

Examples

#include <iostream>
usign namespace std;
int main()
    {             
          int a =10; //normal integer variable
          int *b; // pointer integer variable             
          b =&a;   //b is the pointer variable which is storing the address of a variable
    }

How to use a pointer variable?

  • First, we need to define the pointer data type and its variable name.
  • Then we need to store the address of another variable to that pointer variable, and remember another variable data type should be similar to the pointer variable data type.
  • Now using the pointer asterisk (*) sign we can access another variable value using the pointer variable name.

The main objective of the pointer signs to access the variable data value which addresses the pointer variable is holding, in sort, the pointer is capable of accessing the data value using the address.

Example

#include <iostream>
usign namespace std; 

int main()

   {             
        int nor_var = 100 ;       //normal integer variable
        int *ptr_var;             //integer pointer varaible which can hold address of integer variable              
        ptr_var = &nor_var ;      //pointer variable holding the address of variable nor_var.

        cout<<"The address of nor_val variable is: "<<ptr_var;
        cout<<endl;               // to print new line

        //using the ptr_var access the value of nor_var

        cout<<"The value of *ptr_var is: "<<*ptr_var;
    }

Output

The address of nor_val variable is: 0x6ffe04
The value of *ptr_var is: 100

Behind the Code

Here in this example, we have a normal integer type variable int nor_var =100; and the integer pointer variable int *ptr_var; The ptr_var is only capable of storing the address of integer type variable.

Using this  ptr_var = &nor_var; statement we store the address of nor_var in ptr_var. Now the value of ptr_var is the address of nor_var and now using the pointer asterisk mark * we can access the value of nor_var using *ptr_var.

Array vs Pointer

Often Array gets confused with pointers because of both deals with address and values. We all known to access the element from an array we use its index value and to access the value of another variable we use the asterisk (*) sign before the pointer variable. The concept of both array and pointer is similar.

  • In an array, if we do not pass the index value along with the array or we just write the array name then it represents the base address of that array, which is the address of the first element of the array.
  • This is the similar concept in the pointer, the pointer variable name represents the address of another variable.

Example

#include <iostream>
usign namespace std;
int main()
     {                       
            int arr[4]={12,23,434,23};
            int var=10;
            int *ptr = &var;                       

            cout<<"The address of var is: "<<ptr;
            cout<<endl;
                             
            cout<<"The address of array arr is: "<<arr;
            cout<<endl;
  
            cout<<"The address of first element of array is: "<<&arr[0];         
      }

Output

The address of var is: 0x6ffdfc
The address of array arr is: 0x6ffe00
The address of first element of array is: 0x6ffe00

Behind the Scene

Here you can see that the value of arr is similar to &arr[0], which means arr works as a pointer variable that holds the address of the array base and the index value act as an asterisk sign which helps to access the value which is stored at that address.

Summary

  • & sign is used to tell the address of the variable.
  • * used as the pointer sign.
  • The pointer variable name is used to hold the address of another variable.
  • The data type of pointer variable should be similar to another variable in which the address is stored in the pointer variable.
  • The array name represents the base address of the array itself.

Leave a Reply

Your email address will not be published. Required fields are marked *