Pointers to Pointers
A pointer that stores the address of another pointer:
int a = 10;
int *p = &a;
int **pp = &p;
cout << **pp; // outputs 10
Used in multi-level data structures.
Pointers and Functions
Pointers can be passed to functions to modify values directly.
void update(int *x) {
*x = 20;
}
int main() {
int a = 10;
update(&a);
cout << a; // 20
}
Also useful for dynamic memory operations.
Pointer to Function
Functions have addresses and can be assigned to pointers.
int add(int a, int b) { return a + b; }
int (*funcPtr)(int, int) = add;
cout << funcPtr(2, 3); // 5
Used in callbacks and event handling.
Constant Pointers vs Pointer to Constant
- Constant Pointer: Pointer cannot change the address.
int a = 5;
int *const ptr = &a;
- Pointer to Constant: Cannot change the value through the pointer.
const int *ptr = &a;