C: Advanced Pointers
Pointers are like address slips — a pointer to a pointer is an address slip written on another address slip. It sounds convoluted, but in real life, "the locker number where you store a package" works exactly this way, layer upon layer.
1. Pointer to Pointer
A pointer variable is itself a variable — it lives in memory and has its own address. When you use a pointer to point to another pointer, you get a "pointer to pointer."
// Variable declaration
int x = 42;
// Variable declaration
int *p = &x;
int **pp = &p;
Through **pp you can indirectly modify the value of x:
**pp = 100;
// I/O operation
printf("%d\n", x);
100
The most common use of a double pointer is "modifying the pointer itself inside a function," such as having a function allocate memory for a pointer:
void alloc_buf(char **ptr, int size) {
*ptr = (char *)malloc(size);
}
int main(void) {
char *buf = NULL;
alloc_buf(&buf, 128);
if (buf) {
strcpy(buf, "hello");
printf("%s\n", buf);
free(buf);
}
return 0;
}
hello
char *ptr, the function modifies a copy — the original pointer outside won't change. To modify the pointer itself, you must pass its address.
2. Pointer Array vs. Array Pointer
These two concepts are easily confused. The key is the precedence of * and [].
(1) Pointer Array
int *arr[4] — binds with [] first, so it's an array whose elements are int *.
// Variable declaration
int a = 10, b = 20, c = 30;
int *arr[3] = {&a, &b, &c};
// For loop
for (int i = 0; i < 3; i++) {
// I/O operation
printf("%d ", *arr[i]);
}
10 20 30
A typical application: an array of strings.
const char *names[3] = {"Alice", "Bob", "Carol"};
// For loop
for (int i = 0; i < 3; i++) {
// I/O operation
printf("%s\n", names[i]);
}
Alice
Bob
Carol
(2) Array Pointer
int (*p)[4] — binds with * first, so it's a pointer pointing to an array of 4 ints. Commonly used for passing 2D arrays to functions.
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int (*p)[4] = matrix;
// I/O operation
printf("%d\n", p[1][2]);
7
▶ Example
▶ Try it Yourself 10 20 30 40
50 60 70 80
apple banana cherry
int *a[4] is a pointer array (an array of pointers), int (*a)[4] is an array pointer (a pointer to an array).
3. Function Pointer and Callback
A function name is the entry address of that function. A function pointer can store this address, enabling "deferred calls" or "callbacks."
(1) Function Pointer Declaration
int (*pf)(int, int);
This declares pf as a pointer to a function that takes two ints and returns an int.
// Function definition
int add(int a, int b) { return a + b; }
// Function definition
int sub(int a, int b) { return a - b; }
// Function definition
int main(void) {
int (*pf)(int, int) = add;
// I/O operation
printf("%d\n", pf(3, 5));
pf = sub;
// I/O operation
printf("%d\n", pf(10, 4));
// Return result
return 0;
}
8
6
(2) Callback Mechanism
Pass a function pointer as an argument to another function, so the latter can "call it back" at the right time.
// Function definition
void process(int *arr, int len, int (*transform)(int)) {
// For loop
for (int i = 0; i < len; i++) {
arr[i] = transform(arr[i]);
}
}
// Function definition
int double_it(int n) { return n * 2; }
// Function definition
int negate(int n) { return -n; }
// Function definition
int main(void) {
int data[4] = {1, 2, 3, 4};
process(data, 4, double_it);
// For loop
for (int i = 0; i < 4; i++) printf("%d ", data[i]);
// I/O operation
printf("\n");
process(data, 4, negate);
// For loop
for (int i = 0; i < 4; i++) printf("%d ", data[i]);
// Return result
return 0;
}
2 4 6 8
-2 -4 -6 -8
▶ Example
▶ Try it Yourself3 7 19 42 55
55 42 19 7 3
qsort is a classic application of function pointer callbacks — you only need to provide the comparison rule, and the sorting algorithm takes care of the rest.
4. Three Combinations of const and Pointers
const combined with pointers has three positions, each with different semantics.
(1) Pointer to const
const int *p;
int const *p;
You cannot modify the pointed-to value through p, but p itself can point elsewhere.
// Variable declaration
int a = 10, b = 20;
const int *p = &a;
// I/O operation
printf("%d\n", *p);
p = &b;
// I/O operation
printf("%d\n", *p);
10
20
(2) const Pointer
int * const p = &a;
p cannot change what it points to, but you can modify the value through p.
// Variable declaration
int a = 10;
int * const p = &a;
*p = 99;
// I/O operation
printf("%d\n", a);
99
(3) const Pointer to const
const int * const p = &a;
Neither the pointer's target nor the value it points to can be changed — the strictest read-only.
const is to the left or right of *. On the left, it modifies the data; on the right, it modifies the pointer itself.
5. void Pointer
void * is a "generic pointer" that can point to any type, but you must cast it before use.
// Variable declaration
int a = 42;
// Variable declaration
double b = 3.14;
// Variable declaration
void *p;
p = &a;
// I/O operation
printf("%d\n", *(int *)p);
p = &b;
// I/O operation
printf("%.2f\n", *(double *)p);
42
3.14
Typical uses of void *:
mallocreturnsvoid *, which can be assigned to any pointer typeqsort's comparison function usesconst void *parameters- Implementing generic data structures (e.g., a generic linked list)
void * — the compiler doesn't know the size of the data it points to. You must first cast it to a concrete pointer type.
▶ Example 3: Function Pointer Callback
▶ Try it Yourself输出:
TEXTa=2, b=5
❓ FAQ
return_type (*pf)(param_types), assign with pf = func_name, and call with pf(args) or (*pf)(args). They let you switch which function to call at runtime — the callback pattern in this tutorial is the most common practical use.p points to an int with value 42, *p gives you 42. For a pointer-to-pointer pp, dereferencing once gives the inner pointer, and dereferencing twice gives the actual value.p+1 is undefined. You must first cast it to a concrete type like unsigned char * or int * before doing arithmetic.struct Student every time unless you typedef it. typedef struct Student Student lets you write just Student. This is purely syntactic sugar with no runtime cost — C++ lets you omit struct without typedef, but C does not.📖 Summary
- A double pointer
int **ppis used to modify a pointer itself inside a function - A pointer array
int *a[N]is an array of pointers; an array pointerint (*p)[N]is a pointer to an array - A function pointer
int (*pf)(int,int)stores a function address, and combined with callbacks enables flexible design constto the left of*modifies the data; to the right of*it modifies the pointer itselfvoid *is a generic pointer — you must cast it before use
📝 Exercises
- Write a function
void swap_ptr(int **a, int **b)that swaps what two pointers point to. Verify in main that each pointer points to the other's original value after the swap - Declare a function pointer array
int (*ops[4])(int,int)containing add, subtract, multiply, and divide functions. Call the corresponding operation based on a user-input index - Write a generic print function
void print_any(void *data, char type)where type is 'i' for int, 'd' for double, and 's' for string