Software Developer
The void pointer in c++ is a pointer definitely that has no statistics type related to it. This void pointer can maintain the deal with of any records kind and it could be typecast to any facts kind. While we are talking approximately void pointer we got a doubt length for memory allocation. The void pointer size varied from system to device. If the system configuration is sixteen bit then the size of the void pointer is 2 bytes. If the gadget configuration is 32 bit then the dimensions of the void pointer are 4 bytes and if the system configuration is 64 bit then the dimensions of the void pointer turn into 8 bytes.
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);
This void pointer is used with the * operator before of void keyword.
int A=12;
void *pointer=&a;// pointer holds the address of the "A" variable
Here are the following examples mentioned below:
//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;
}
//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;
}
//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;
}
//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;
}