×

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

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

How to Setup Environment for C++ Programming on Mac

Mac OS X code Installation There are so many environments available for C++. We are going to install jGrasp and Xcode in our mac operating system. Instruction for installation of jGrasp and...

2 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.