Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Void Pointers

In this tutorial, we will learn about void pointers and how to use them with the help of examples.

Before you proceed with this tutorial, be sure to check C++ pointers.

Void Pointer

  • A void pointer is A pointer that has no associated data type with it.
  • A void pointer is a general purpose pointer that can hold address of any type and can be typecasted to any type.
  • The void pointer doesn’t mean it points to nothing. Actually, it is pointing something but we just don’t know the exact type of object it is pointing to.

Syntax

void *ptr;

In C++, we cannot assign the address of a variable of one data type to a pointer of another data type.

Consider this example:

// pointer is of int type
int *ptr;

// variable is of double type
double d = 2.0;

// Error occurs
// we can't assign double* to int*
ptr = &d;

Here, the error occurred because the address is a double type variable. However, the pointer is of int type.

In such situations, we can use the void pointers (pointer to void) in C++.

For example:

// void pointer
void *ptr;
                                                                      
double d = 2.0;
                                                                      
// valid statement
ptr = &d;

We discuss earlier in this tutorial, The void pointer is a generic pointer that is used when we don’t know the data type of the variable that the pointer points to.


Example 1: C++ Program how to use Void Pointer

#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 2.0f;

    // assign float address to void
    ptr = &f;

    cout << "The address of floating point no.: " << &f << endl;
    cout << "The address of void pointer no.: " << ptr << endl;

    return 0;
}

Output

The address of floating point no.: 0x7ffe1a5a540c
The address of void pointer no.: 0x7ffe1a5a540c

Here, the pointer ptr is given the value of &f.

The output shows that the void pointer ptr stores the address of a float variable f.


As void is an empty type, void pointers cannot be dereferenced.

void* ptr;
float* fptr;
float f = 2.0;
                                                                      
// assign float address to void pointer
ptr = &f;
cout << *ptr << endl;  // Error Occurs

// assign float address to float pointer
fptr = &f;
cout << *fptr << endl;   // Valid value

Example 2: Printing the Content of Void Pointer

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 2.0f;
  
    // assign float address to void pointer
    ptr = &f;
  
    cout << "The Value of void pointer: ";
    // use type casting to print pointer content
    cout << *(static_cast <float*>(ptr));
  
    return 0;
  }

Output

The value of void pointer: 2.0

This program prints the value of the address pointed to by the void pointer ptr.

Since we cannot dereference a void pointer, we cannot use *ptr.

However, if we convert the void* pointer type to the float* type, we can use the value pointed to by the void pointer.

In this example, we have used the static_cast operator to convert the data type of the pointer from void* to float*.


Example 3: Print the Void Pointer using type casting

#include <iostream>
using namespace std;
int main() {
    void* ptr;// void pointer declaration
    int* ptr1;// integer pointer declaration
    int data = 2;// integer variable initialization
  
    ptr = &data; // storing the address of data variable in void pointer variable
    ptr1 = (int*)ptr; // assigning void pointer to integer pointer
    
    cout << "The Value of *ptr1: " << *ptr1 << endl;
  
    return 0;
  }

Output

The value of *ptr1: 2

Working

  • In this above program, we declare two pointer variables of type void and int type respectively.
  • We also create another interger type variable data.
  • After declaration, we store the address of variable data in a void pointer variable ptr.
  • Now, we want to assign the void pointer to integer pointer, in order to do this, we need to apply the cast ooperator ie,. int to the void pointer variable.
  • This cast operator tells the compiler which type of value void pointer is holding.
  • For casting, we have to type the data type and * in a bracket like (char) or (int).

Note: void pointers cannot be used to store addresses of variables with const or volatile qualifiers.

void *ptr;
const double d = 2.0;

// Error occurs: Invalid conversion from const void* to void*
ptr = &d;
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!