const Pointers, Pointers to const & void*

🟡 Intermediate

📖 Definition

A pointer to const (const T*) prevents modifying the pointed-to object. A const pointer (T* const) prevents reassigning the pointer address. A generic pointer (void*) holds raw memory addresses without type information.

💡 Code Example

#include <iostream>

int main() {
    int x = 100;
    const int* p1 = &x; // Cannot change *p1
    int* const p2 = &x; // Cannot change p2 address

    std::cout << "*p1: " << *p1 << ", *p2: " << *p2 << "\n";
    return 0;
}