C++ Generic Pointer
In C++, a generic pointer is a pointer of type void*
, which can point to any data type. It is often used when the exact type of data the pointer will store is not known at compile time or when writing functions that need to work with multiple data types.
Since void*
does not have a specific data type, it cannot be dereferenced directly without typecasting.
Syntax
The syntax to declare a generic pointer is:
</>
Copy
void* pointer_name;
- void*
- A pointer type that can store the address of any data type.
- pointer_name
- The name of the generic pointer variable.
Examples
Example 1: Storing and Printing Values of Different Types
This example demonstrates how to use a generic pointer to store and print values of different data types.
</>
Copy
#include <iostream>
using namespace std;
void printValue(void* ptr, char type) {
switch (type) {
case 'i':
cout << "Integer: " << *(static_cast<int*>(ptr)) << endl;
break;
case 'f':
cout << "Float: " << *(static_cast<float*>(ptr)) << endl;
break;
case 'c':
cout << "Character: " << *(static_cast<char*>(ptr)) << endl;
break;
}
}
int main() {
int num = 42;
float pi = 3.14;
char letter = 'A';
printValue(&num, 'i');
printValue(&pi, 'f');
printValue(&letter, 'c');
return 0;
}
Output:
Integer: 42
Float: 3.14
Character: A
Explanation:
- The function
printValue
takes avoid*
pointer and a type identifiertype
as parameters. - Inside the function, the
switch
statement checks thetype
parameter to determine the data type of the value. - The
void*
pointer is typecast to the appropriate data type usingstatic_cast
before dereferencing. - In the
main
function:- The address of an integer variable is passed with type
'i'
. - The address of a float variable is passed with type
'f'
. - The address of a character variable is passed with type
'c'
.
- The address of an integer variable is passed with type
- The function correctly prints the value stored at the pointer’s address for each data type.
Example 2: Using Generic Pointers for Arrays
This example demonstrates using a void*
pointer to access elements of an integer array.
</>
Copy
#include <iostream>
using namespace std;
void printArray(void* arr, int size) {
int* intArr = static_cast<int*>(arr); // Cast to int*
for (int i = 0; i < size; ++i) {
cout << "Element " << i << ": " << intArr[i] << endl;
}
}
int main() {
int numbers[] = {10, 20, 30, 40};
printArray(numbers, 4);
return 0;
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Explanation:
- The function
printArray
takes avoid*
pointer and the size of the array as parameters. - Inside the function, the
void*
pointer is cast to anint*
pointer. - A loop iterates over the array, and each element is accessed and printed using the dereferenced pointer.
- In the
main
function, the address of the arraynumbers
is passed toprintArray
. - The function correctly prints all elements of the array.
Key Points about Generic Pointers
- A generic pointer (
void*
) can store the address of any data type. - It cannot be dereferenced directly. The pointer must first be typecast to the appropriate type.
- Generic pointers are commonly used in scenarios where the data type is determined at runtime, such as in dynamic memory allocation and generic programming.
- Care must be taken to ensure the correct typecasting to avoid undefined behavior.