GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — void Pointer in C++ - GeeksforGeeks void Pointer in C++Last Updated : 18 Jun, 2026 A void pointer (void*) is a special type of pointer that can store the address of any data type. Since it is not associated with a specific type, it is also known as a generic pointer.
Output The value of a is: 10 The Adress of a is 0x7ffd9ac02a64 Explanation: The void pointer stores the address of an integer variable. Since it does not know the data type of the object it points to, only the address can be printed directly. Syntax
Here,
Dereferencing a Void PointerA void pointer cannot be dereferenced directly because the compiler does not know the type and size of the object it points to. Therefore, it must first be converted to the appropriate pointer type.
Output 50 Explanation: The void pointer is first converted to an int* using static_cast, after which it can be safely dereferenced. Using Void Pointer with Different Data TypesOne of the biggest advantages of a void pointer is that the same pointer can point to different types of variables.
Output 10 25.5 $ Explanation: The same void pointer stores the addresses of variables of different types. Before accessing each value, it is typecast to the corresponding pointer type. Application of Void PointerA void pointer is commonly used when the exact data type is not known beforehand or when the same piece of code needs to work with multiple data types. Generic ProgrammingOne of the most common uses of a void pointer is writing generic code. Unlike typed pointers, which can point to only one data type, a void pointer can store addresses of different data types, making code more reusable. Example: Using an int Pointer
Output error: cannot convert ‘float*’ to ‘int*’ in assignmentExplanation: An int* pointer can only store the address of an integer variable. Assigning it the address of a float results in a compilation error. A void pointer removes this restriction. Example: Using a Void Pointer
Output Address of n: 0x7fffffffe9c4 Address of f: 0x7fffffffe9c0 Address of c: 0x7fffffffe9bf Explanation: The same void pointer is reused to store the addresses of variables of different data types. Since a void pointer has no associated type, it can point to any object. However, to access the stored value, it must first be typecast to the correct pointer type. Dynamic Memory AllocationVoid pointers are useful when dynamically allocated memory needs to be handled in a generic way. After allocating memory, the pointer can be converted to the required type before accessing the allocated object.
Output Value from allocated memory: 42 Explanation: Memory is allocated and its address is stored in a void pointer. After casting it to int*, the allocated memory can be accessed and modified normally.
Callback FunctionsVoid pointers are widely used in callback functions and generic APIs where the type of data passed to the callback is determined at runtime. A single callback function can process different data types by casting the void pointer appropriately.
Output Callback for integer: 7 Callback for double: 8.12 Explanation: The callback function receives a void pointer along with information about the actual data type. It casts the pointer to the appropriate type before accessing the value, allowing the same callback to work with multiple data types. Advantages of Void Pointer in C++Void pointers provide flexibility when writing generic code and working with APIs that handle multiple data types.
Limitations of Void Pointer in C++Although flexible, void pointers should be used carefully because they sacrifice type safety.
Comment ExploreStandard Template Library(STL) Practice & Problems |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |