Overview of C++ Void Pointer

Overview of C++ Void Pointer
Techiio-author
Written by Sagar RabidasJanuary 31, 2022
7 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion with C++ Void Pointer.

C++ Void Pointer

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.

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++?

This void pointer is used with the * operator before of void keyword.

Syntax:

int A=12;
void *pointer=&a;// pointer holds the address of the "A" variable

Examples of C++ Void Pointer

Here are the following examples mentioned below:

Example #1 – Void pointer for int, float, and String

Code: VoidPointer.CPP

//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;
}

Example #2 – Trying to convert the void pointer to constant

Code: VoidPointerConstant.CPP

//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;
}

Example #3 – Pointer to void

Code: PointerVoid.CPP

//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;
}

Example #4 – Memory allocation with malloc method

Code: MemoryAllocationMalloc.CPP

//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;
}

C
C++
C++ Void Pointer
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance