×

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type.

Void Pointer Syntax:

void *ptr;  

We can't assign a variable's address to a variable of a different data type in C++.To better understand this let see an example below:

int *ptr;  //declaration of an integer pointer ptr 
float i=14.6; // initialization of a floating variable i
ptr= &i;  // This statement throws an error.

Explanation:

We define a pointer of type integer, ptr, and a float variable, 'i', in the preceding example. We try to save the address of 'i' variable in 'ptr' after declaration, but this is not feasible in C++ since the variable cannot carry the addresses of various data types.

Another example of a program containing void pointer in C++:

#include <iostream> 
#inlcude <bits/stdc++.h> 
using namespace std; 
int main()  
{  
    int *ptr;  
    float i= 14.6;  
    ptr = &i; // error  
    std::cout << "The value of *ptr is : " <<*ptr<< std::endl;  
    return 0;  
}

OUTPUT:

error: cannot convert ‘float*’ to ‘int*’ in assignment
ptr = &f; //error

Explanation:

In the above program, we define an integer pointer and a float variable. An integer pointer variable cannot point to a float variable, but it can only point to an integer variable. The above problem was solved in C++ by utilizing the void pointer, which may contain the address of any data type.

Another example of a program containing void pointer in C++:

#include <iostream>  
#include <bits/stdc++.h>
using namespace std;  
int main()  
{  
  void *ptr;   // declaration of a void pointer 
  int i=19;   //  initialization of an integer variable
  ptr=&i;   // storing the address of 'i' variable in a void pointer variable ptr.  
  std::cout << &i << std::endl;  
  std::cout << ptr << std::endl;  
  return 0;  
}

OUTPUT:

0x61ff08
0x61ff08
press any key to continue……

Explanation:

We declare void pointer variable and integer variable in the preceding program, which contains the address of void pointer integer variable.

Advantages of Void Pointer in C++

  • Because malloc() and calloc() return the void * type, they may be used to allocate memory of any data type (due to the void *).
  • Normal functions in C are executed using void pointers. For example, in qsort, the compare function is used ().

Important fact: It is not possible to ignore void pointers. The following program, for example, does not compile.

#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
int main()
{	int i = 10;
	void *ptr = &i;
	printf("%d", *ptr);
	return 0;
}

OUTPUT:

Compiler error: 'void*' is not a pointer-to-object type

Following program run fine with the compiler:

#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
int main()
{int i = 20;
void *ptr = &i;
printf("%d", *(int *)ptr);
return 0;
}

OUTPUT:

20

Related Topics

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.