What is void pointer? Explain with suitable C++ program.
Use of
void Pointer
•
The void pointer is a special type of pointer that can be used to point to the
objects of any data type. It is also known as generic pointer.
•
The void pointer is declared like normal pointer declaration, using the keyword void.
•
For example:
void
*ptr;
•
Following is a simple C++ program in which we have used void pointer. The
address of interger variable is assigned to void pointer. And finally using
this void pointer the integer data can be accessed.
#include<iostream>
using namespace std;
void main()
{
//As the void pointer does not know
//what type of object it is pointing to,
//it can not be dereferenced!
//Hence, the void pointer must first be explicitly cast to
another pointer type
//before it is
dereferenced.
int int_val = 10;
void *pVoid = &int_val;
// can not dereference pVoid because it is a void pointer
int *pInt = static_cast<int>(pVoid); // cast from void* to
int*
cout << *pInt << endl; // can dereference pInt
}
Output
10
Review Questions
1.What is void
pointer? Explain with suitable C++ program.
Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Use of void Pointer
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation