In C++ programming language, a pointer seems to be a variable that can save or hold the address of another variable and be utilized to refer toward any other extra pointer. The two different usages of pointers are to save memory and accelerate the processor’s speed.
Types of Pointers in C++
Several types of pointers in the C++ programming language are shared and explained below:
Null Pointer
A null pointer can be created or executed if we provide a “NULL” value while declaring the pointer. This method is found to be useful if the pointer has been assigned no values at all. Inside a null pointer, 0 is always present as a pointer value. Let’s look at an illustration of using the Null Pointer in code.
using namespace std;
int main()
{
int *i = NULL;
cout << "The value of variable i: " << i;
return 0;
}
At the commencement of the code, the header file <iostream> would be integrated. The standard namespace “std” would then be used. Then, the main() method would be executed. Here, the pointer of the variable “i” would be declared. The value of this pointer would be assigned as “NULL”. Next, the “cout” command would be utilized to show the outcome. To terminate the program, we would employ a return 0 statement.
Void Pointer
There is no standard data type for the void pointer. This kind of pointer is usually created using the term “void”. One of the main uses of this pointer is to store or keep the address of some other variable.
using namespace std;
int main()
{
void *z = NULL;
cout << "The size of z: " << sizeof(z);
return 0;
}
The <iostream> header file would be integrated at the start of the program. Further, we will utilize the default namespace as “std”. The main() method is being called. The variable “z” pointer will be declared in the next step. We would be assigned the value “NULL” for this pointer. The “cout” command would display the size of the required pointer. The sizeof() function would be utilized to determine the pointer’s size. Using the return 0 statement, we might end the program.
Pointer to Pointer
We already discussed that in C++, a pointer is utilized to hold a variable’s address. A pointer speeds up a variable’s access. To hold the address of some other pointer, a pointer may also be defined in C++. In a pointer to pointer function, the variable address is stored in the first pointer. On the other hand, the first pointer address is usually stored in the second or some other pointer.
Syntax
Example # 1
Now, we will see the usage of a pointer to pointer in C++.
using namespace std;
int main()
{
int x = 16;
int *m;
int **mm;
m = &x;
mm = &m;
cout << "The address of the variable x: " << m <<endl;
cout << "The address of the variable m: " << mm <<endl;
cout << "The value stored in the variable m: " << *m <<endl;
cout << "The value stored in the variable mm: " << **mm <<endl;
}
The program will be started by integrating the header file <iostream>. This library will be applied for executing different input and output functionalities. First, we would initialize the variable “x” and assign it the value. The pointer that saves the address of the variable “x” will be declared in the next step. We would then create another pointer. Here, we would indicate the first pointer to the address of the “x”. The value of the first pointer, “m”, would be where the double-pointer would point. We would call the “cout” statement four times. The first two “cout” commands print the address of the specified variable and pointer.
Example # 2
Let’s look at an illustration of a pointer that references another pointer by address.
using namespace std;
int main()
{
int v[12] = {102, 212, 870, 169, 581, 721};
int *d[] = {v, v+1, v+2, v+3, v+4, v+5};
int **dd = d;
dd++;
cout << dd-d,*dd - v,**dd;
*dd++;
cout << dd-d,*dd - v,**dd;
++*dd;
cout << dd-d,*dd - v,**dd;
++**dd;
cout << dd-d,*dd - v,**dd;
}
The library <iostream> would be incorporated at the commencement of the code. This module contains the input and output methods. Now, we would start the coding inside the body of the main() function. Here, we first created the array. This array contains six values. These values are stored in the variable “v”.
Then, we declare the array for the pointer named “d”. In the next step, we would declare a double pointer. Then we could increment the value of the pointer. The double pointer is employed with pointer arithmetic. It is constructed as a 6-element array with an array of pointers (d) is defined. The pointers referring to each member of the array make up the elements of “d”. Because the base address of the array is contained in the array’s name, it will function as a pointer, and its value may be accessed utilizing *(v), *(v+1), *(v+2), *(v+3), *(v+4), *(v+5). In the end, we would apply the “cout” command to display the outputs.
Advantages of Using the Pointers to Pointers in C++
- Accessing memory locations is made easier with pointers.
- An array structure’s components can be efficiently accessed via pointers.
- Both deallocation and dynamic memory allocation require pointers.
- Complex data structures, like linked lists, graphs, trees, etc., are created using pointers.
Disadvantages of Using the Pointers to Pointers in C++
- It might be challenging to comprehend pointers.
- Pointers can cause several issues, including segmentation faults and unauthorized memory access.
- Memory corruption might result from giving a pointer an inaccurate value.
- Memory leaks can also occur through pointers.
- Pointers will initialize more slowly than variables.
- Because manipulating pointers may be exceedingly challenging for programmers, they are required to do it wisely.
Conclusion
In this article, we discussed the pointers used in the C++ programming language, its advantages, disadvantages, syntax, and applications. Further, we discussed the pointer to pointer in the C++ language. Additionally, we executed different programs to demonstrate how and when to use pointers to pointers in C++. We also see how to use the Void and NULL pointer in C++ by implementing the codes.