C Language
7. Pointers & Memory ManipulationAdvanced12 min read

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.

Key Takeaways & Core Concepts
  • 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 test if (p != NULL) before dereferencing.
  • Void Pointer (`void):** Generic untyped pointer used in malloc() and qsort()`. 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.
Dangling Pointer Prevention

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.

Function Pointer Callback Demonstration
1#include <stdio.h>
2
3int multiply(int a, int b) { return a * b; }
4int add(int a, int b) { return a + b; }
5
6// High-order function accepting a function pointer as a callback
7void compute(int x, int y, int (*operation)(int, int), const char *opName) {
8 int result = operation(x, y);
9 printf("%s of %d and %d = %d\n", opName, x, y, result);
10}
11
12int main(void) {
13 // Declaring a double pointer
14 int val = 999;
15 int *p = &val;
16 int **pp = &p; // Pointer to pointer
17
18 printf("Double pointer resolution: **pp = %d\n\n", **pp);
19
20 // Invoking callbacks via function pointers
21 compute(6, 7, multiply, "Multiplication");
22 compute(6, 7, add, "Addition");
23
24 return 0;
25}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
25 lines
Note: compute() doesn't need to know which math function runs; it executes whatever function pointer is passed.
Practice & AssessmentC Programming Complete Course

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)

1

Can you directly dereference a void pointer (e.g., *voidPtr) in standard C?

Advanced