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:

  1. The function printValue takes a void* pointer and a type identifier type as parameters.
  2. Inside the function, the switch statement checks the type parameter to determine the data type of the value.
  3. The void* pointer is typecast to the appropriate data type using static_cast before dereferencing.
  4. 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'.
  5. 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:

  1. The function printArray takes a void* pointer and the size of the array as parameters.
  2. Inside the function, the void* pointer is cast to an int* pointer.
  3. A loop iterates over the array, and each element is accessed and printed using the dereferenced pointer.
  4. In the main function, the address of the array numbers is passed to printArray.
  5. The function correctly prints all elements of the array.

Key Points about Generic Pointers

  1. A generic pointer (void*) can store the address of any data type.
  2. It cannot be dereferenced directly. The pointer must first be typecast to the appropriate type.
  3. Generic pointers are commonly used in scenarios where the data type is determined at runtime, such as in dynamic memory allocation and generic programming.
  4. Care must be taken to ensure the correct typecasting to avoid undefined behavior.