Advanced Pointers: Double Pointers, Void & Function Pointers
Master pointer to pointer (**ptr), void pointers (generic memory), NULL pointers, dangling pointers, and function pointers for callbacks.
- A double pointer (
int **) stores the memory address of another pointer. - A
void*pointer is a generic pointer that can point to any type without a cast, but cannot be dereferenced directly. - A NULL pointer points to memory address 0, signaling that it does not currently reference valid data.
- A dangling pointer points to memory that has already been deallocated or freed.
- Function pointers store the entry address of executable code, enabling callbacks and event handling.
Special Pointer Types: NULL, Void, Wild & Dangling
Understanding pointer safety prevents major vulnerabilities (segmentation faults, arbitrary code execution):
- NULL Pointer: Explicitly assigned
NULL(int *p = NULL;). Always testif (p != NULL)before dereferencing. - Void Pointer (`void
):** Generic untyped pointer used inmalloc()andqsort()`. Must be cast to a concrete type before dereferencing. - Wild Pointer: An uninitialized pointer holding arbitrary garbage values.
- Dangling Pointer: A pointer still holding the address of a variable or heap block that has been
free()d or gone out of scope.
Immediately after calling `free(ptr);`, always set `ptr = NULL;`. Dereferencing NULL immediately triggers a clean crash rather than silent data corruption.
Function Pointers and Callback Architecture
Just as variables reside in memory, compiled function code resides in the Text segment. A function pointer points to the first instruction of a function.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 1 correct(0 attempted)
Can you directly dereference a void pointer (e.g., *voidPtr) in standard C?
Advanced