×
>
<

Aptitude

C++ Pointer

What is a Pointer

The pointer in C++ is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.
The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Declaring a Pointer

The pointer in C++ language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

Declaration
int *x; //pointer to int  
char *y; //pointer to char      

By the help of * (indirection operator), we can print the value of pointer variable p.

Example :

#include <iostream>
using namespace std;

int main()
{
    int number=15;    
    int *p;      
    p = &number; //stores the address of number variable    
    cout << "Address of number variable is:"<< &number<< endl;    
    cout << "Address of p variable is:"<< p << endl;    
    cout <<" Value of p variable is:"<< *p << endl;    
    return 0;   
}     

Output :

Address of number variable is:0x7ffccc8724c4
Address of p variable is:0x7ffccc8764c4
Value of p variable is:15
Facts About Pointer

Advantages

  • Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
  • We can return multiple values from a function using the pointer.
  • It makes you able to access any memory location in the computer's memory.

Uses of Pointer

1) Dynamic memory allocation

In C++ language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in C++ language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

What is a pointer to a pointer?
In C++, we have the ability to build a pointer to another pointer, which might then point to data or another pointer. The unary operator (*) is all that is needed in the syntax for declaring the pointer for each level of indirection..
  
char a;  
char *b;  
char ** c;  
a = 'g';  
b = &a;  
c = &b;    
  
Types of Pointers
There are majorly four types of pointers, they are :
  • Null Pointer
  • Void Pointer
  • Wild Pointer
  • Dangling Pointer

1.Null Pointer

A pointer refering NULL is known as null pointer.
Syntax :
  
Int *var = NULL;
  
Example :
  
#include <iostream>
using namespace std;

int main()
{
    int *var = NULL;
    cout << "var = " << *var;
}   
  
Segmentation fault  

2.Void Pointer

When a pointer is declared with a void keyword, then it is called a void pointer. We need to type cast it first if we want to print it.
Syntax :
  
void *var;
  
Example :
  
#include <iostream>
using namespace std;

int main()
{
    int a=2;
    void *ptr;
    ptr = &a;
    cout << "After Typecasting, a = " >> *(int *)ptr;
    return 0;
}   
  

Output :

After Typecasting, a = 2  

3.Wild Pointer

A wild pointer is a pointer which is only declared but not assigned to any address of variable. Which may cause segmentation errors..
Syntax :
  
int *ptr;
  
Example :
  
#include <iostream>
using namespace std;

int main()
{
    int *ptr;
    cout << "ptr=" << *ptr);
    return 0;
}   
  

Output :

Segmentation fault

4.Dangling Pointer

Suppose there is a pointer p pointing at a variable at memory 100005. If we deallocate this memory, then this p is called a dangling pointer.
We can deallocate a memory using a free() function in C.
Syntax :
  
int *ptr;
  
Example :
  
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int *ptr=(int *)malloc(sizeof(int));
    int a=5;
    ptr = &a;
    free(ptr); //now this ptr is known as dangling pointer.
    cout << "After deallocating its memory *ptr = " << *ptr);
    return 0;
}   
  

Output :

free(): invalid pointer
Aborted