Pointers in C++

In C++ programming, a pointer is a variable that is used to store the address of another variable of the same type. They are very useful to pass the value as a reference in the function (also known as call by reference). It points to the memory location where the first byte of the object is stored.

Syntax for Declaration of Pointer Variable.

syntax: data_type * pointer_name;
example: int *ptr; 
//ptr can point to the address of a integer type variable


Working of Pointer in C++.

pointer in C++
Define a pointer variable and initialize the pointer variable with the address of variable using unary operator (& also called address of operator) which return the address of the variable.
int x = 5;
int *ptr;
ptr = &x; //assign address of variable x to the pointer variable ptr

The derefrence/indirected operator (*) is an operator that is used to access the value stored at the location pointed by the pointer. It return the value of the variable located at that address specified by the operand. Using * operator we can also change the value of the variable pointed by the pointer. 
int x = 5;
int *ptr = &x;

cout<<*ptr; //answer: 5

*ptr = 10;
cout<<*ptr; //answer: 10

Pointer Example Code:

//C++ Pointer Example
#include<iostream>
using namespace std;

int main(){

    //variable declaration
    int x = 10;
    //pointer vairbale declaration 
    int *ptr;
    //pointer variable initialize
    ptr = &x; 
    
    cout<<"Value of x: "<<x<<endl;
    cout<<"Value of *ptr: "<<*ptr<<endl;
    cout<<"Value of ptr: "<<ptr<<endl;

    return 0;
}
Output:
Value of x: 10
Value of *ptr: 10
Value of ptr: 0xd36fff8d4

Pointer Example in Function (Call by Reference).

//C++ Pointer Example for function
#include<iostream>
using namespace std;

//call by reference
void swapValue(int *x, int *y){
    *x = 30;
    *y = 10;
}
int main(){

    int m = 10, n = 30;
    //function call
    swapValue(&m, &n);

    cout<<"m = "<<m<<endl;
    cout<<"n = "<<n;

    return 0;
}
Output:
m = 30
n = 10

Pointer to Pointer in C++.

Pointer to pointer is a condition in which we store the address of one pointer into another pointer which point the address of actual variable. The pointer to pointer is declare using two derefrencing operator (**).
syntax: int **pptr; //pointer to pointer

Example:
//C++ Pointer to Pointer Example
#include<iostream>
using namespace std;

int main(){

   int value = 30;
   //taking address of value
   int *ptr = &value;
   //taking address of pointer ptr
   int **pptr = &ptr;

   cout<<"Value of value: "<<value<<endl;
   cout<<"Value of *ptr: "<<*ptr<<endl;
   cout<<"Value of **ptr: "<<**pptr<<endl;

   return 0;
}
Output:
Value of value: 30
Value of *ptr: 30
Value of **ptr: 30

Key points to note:
  • Never apply the indirected operator (*) to the uninitialized pointer. 
int *ptr;
cout<<*ptr; //undefined behaviour
  • Never assign value to an uninitialize pointer, it will lead to segmentation fault error.
int *ptr;
*ptr = 1; //Segmentation Fault (SIGSEGV)


⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS