Updated April 13, 2023
Introduction to C++ Void Pointer
The void pointer in C++ is a pointer actually that has no data type associated with it. This void pointer can hold the address of any data type and it can be typecast to any data type. While we are talking about void pointer we got a doubt size for memory allocation. The void pointer size varied from system to system. If the system configuration is 16 bit then the size of the void pointer is 2 bytes. If the system configuration is 32 bit then the size of the void pointer is 4 bytes and if the system configuration is 64 bit then the size of the void pointer becomes 8 bytes.
The void pointer does not mean that it points to nothing. But we don’t know the exact type of the object it is pointing to, it will point to something.
Void pointer(*) type memory allocated by using malloc() and calloc() functions.
Syntax for dereferencing
int a = 10;
void *ptr = &a; // pointer holds the address of the "a" variable
cout << (*(int *)ptr); //dereference the void pointer
malloc() Syntax:
void* malloc(sizesizeBytes);
calloc() Syntax:
void* calloc(size num, size sizeBytes);
How does the void pointer used in C++ with Examples?
This void pointer used with * operator before of void keyword.
Syntax:
int A=12;
void *pointer=&a;// pointer holds the address of the "A" variable
Here are the following examples mention below:
Example #1 – Void pointer for int, float, and String
Code:
//including c++ input and output libraries
#include <iostream>
using namespace std;
//creating enum class
enum DataTypeConstants {
STRING,
FLOAT,
INT,
};
//called showMyVoidPointer for method implementation
//void *pointer is void pointer
void showMyVoidPointer(void *pointer, DataTypeConstants dTypes) {
//checking whether we got int or float or string type with switch case
switch (dTypes) {
case INT:
cout << "Employee ID is: "<<*(int*)pointer << endl;
break;
case FLOAT:
cout << "My Salary is: "<<*(float*)pointer<< endl;
break;
case STRING:
cout << (char*)pointer << endl;
break;
}
}
//main method for run the c++ application
int main()
{
//declaring and initializing the int variable
int empID = 2452;
//declaring and initializing the float variable
float salary = 48000.00;
//declaring and initializing the string variable
char *charValue ="Hi, I am Paramesh";
//calling showMyVoidPointer method for int value
showMyVoidPointer(&empID, INT);
//calling showMyVoidPointer method for float value
showMyVoidPointer(&salary, FLOAT);
//calling showMyVoidPointer method for String value
showMyVoidPointer(charValue, STRING);
return 0;
}
Output:
Example #2 – Trying to convert the void pointer to constant
Code:
//including c++ input and output libraries
#include <iostream>
//main method for run the c++ application
int main()
{
//declaring and initializing the int variable
int first;
//declaring and initializing the int pointer
int *pointerFirst = 0;
//declaring and initializing the int pointer
char *pointerSecond = 0;
//declaring the void pointer
void *pointerVoid;
//initializing void pointer to int
pointerVoid = pointerFirst;
//initializing void pointer to int
pointerVoid = pointerSecond;
const int *pointerConst = &first;
pointerVoid = pointerConst; //trying to assign void pointer to constant, it is not possible so we got error
return 0;
}
Output:
Example #3 – Pointer to void
Code:
//including c++ input and output libraries
#include <iostream>
using namespace std;
//main method for run c++ application
int main() {
//declaring void pointer
void* pointer;
//declaring and initializing float variable
float money = 55.50;
//nitializing vooid pointer variable
pointer = &money; // converting float to void ponter
//displaying output
cout <<&money << endl;//displayed money address, it may varied from system to system
//displaying output
cout << pointer <<endl;//displayed pointer address, it may varied from system to system
return 0;
}
Output:
Example #4 – Memory allocation with malloc method
Code:
//including c++ input and output libraries
#include <iostream>
//including c++ std libraries
#include <cstdlib>
using namespace std;
//main method for run c++ application
int main()
{
//declaring void pointer
int *pointer;
//allocating Memory for pointer by using malloc() method
pointer = (int*) malloc(10*sizeof(int));
//checking whether pointer existed or not
if(!pointer)
{
cout << "Memory Allocation Failed";
exit(1);
}
//display some static content
cout << "Allocating Memory values......" << endl << endl;
//iterating pointer values
for (int var=0; var<5; var++)
{
pointer[var] = var*2+2;
}
cout << "Memory values are--->" << endl;
//iterating pointer values
for (int var=0; var<5; var++)
{
cout << *(pointer+var) << endl;
}
//free the pointer Memory
free(pointer);
return 0;
}
Output:
Conclusion
Void pointer is used to store the address of any other data type. Void pointer is declared with void*. We can’t dereference the void pointer without reassigning this pointer to another variable type.
Recommended Articles
This is a guide to C++ Void Pointer. Here we discuss the introduction to C++ Void Pointer along with syntax for dereferencing and programming examples. You may also have a look at the following articles to learn more –