![]() |
In C++, a void pointer is a pointer that is declared using the ‘void‘ keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a “Generic Pointer“. Syntax of Void Pointer in C++void* ptr_name; As the type of data the void pointer is pointing to is unknown, we cannot dereference a void pointer. Example of Void Pointer in C++The below example demonstrates the declaration and use of void pointer in C++. C++
Output
The value of a is: 10 The Adress of a is 0x7ffd9ac02a64 Application of Void Pointer in C++1. Generic CodingThe concept of a generic pointer means a pointer that can be used to point to data of any data type. This is helpful in situations where you need a single pointer that can handle different data types dynamically. Let’s see an example for this, let’s first declare variables of different types (int, double, char). then declare a void pointer named “generic pointer”. We use the void pointer to point to each of the variables in succession with the help of typecasting. Example Below is a program in which an int type pointer is declared and we try to point the value of a float datatype using it. Let’s See what happens. C++
Output error: cannot convert ‘float*’ to ‘int*’ in assignment Explanation: The above program gives an error the reason is int type pointer ptr is declared and we are trying to store the memory address of a float data type into an int pointer, which gives an error. But if we declare void pointer it allows us to change their data type. So, declare ptr as a void pointer and the program will not give any error. The below example demonstrates the same. C++
Output
The value of n 10 The Adress of n 0x7ffe05023434 The value of n 25.25 The Adress of n 0x7ffe05023430 The value of n $ The Adress of n 0x7ffe0502342f Explanation: In the above Code, We declared a void pointer as the variable name “ptr” which acts as a generic pointer or void pointer. This pointer stores multiple datatype addresses like int, char and float. We Typecasted the void pointer to any data type (float, char, int). Hence Void Pointer allows us to change the addresses for different memory addresses of different data types. 2. Dynamic Memory AllocationDynamic memory allocation is performed in C++, when the size of memory needed is not known at compile time then we perform Dynamic memory Allocation using operators like new or malloc. Void pointers can be used to allocate memory for any data type. In C++, “new” keyword is used for dynamic memory allocation which returns a pointer to the allocated memory. After allocating memory, We need perform type casting to use the allocated memory with a specific data type. Example The below program demonstrate the use of void pointer to dynamically allocate memory for any data type. C++
Output
Value from allocated memory: 42 3. Callback FunctionsFunctions that are passed as arguments to other functions are called callback functions. In case we need to handle multiple data types, the void pointer can be used as a void pointer provides us the flexibility to handle different data types. Callback functions can be defined to accept a void pointer as a parameter, enabling them to handle different types of data. This approach simplifies the code because this approach maintains different data types in a single callback. Example The below program demonstrates the use of the void function to pass a void pointer as a parameter in a callback function. C++
Output
Callback for integer: 7 Callback for double: 8.12 Advantages of void Pointer in C++The void pointer have the following major applications:
ConclusionIn conclusion, void pointers in C++ offer flexibility and versatility in certain programming scenarios, but this demands careful consideration. Modern C++ offers safer alternatives, like smart pointers for memory management. |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |