C: Advanced Pointers

🗓 Last updated: 2026-11-30👤 Written by: Harper✓ Reviewed by: Alex🏷 Category: C Tutorial

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.

Reference entries in this section: qsort const malloc struct

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."

C
// Variable declaration
int x = 42;
// Variable declaration
int *p = &x;
int **pp = &p;

Through **pp you can indirectly modify the value of x:

C
**pp = 100;
// I/O operation
printf("%d\n", x);
TEXT
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:

TEXT
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;
}
TEXT
hello
💡 Tip: If you only pass a single pointer 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 *.

C
// 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]);
}
TEXT
10 20 30

A typical application: an array of strings.

C
const char *names[3] = {"Alice", "Bob", "Carol"};
// For loop
for (int i = 0; i < 3; i++) {
    // I/O operation
    printf("%s\n", names[i]);
}
TEXT
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.

C
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]);
TEXT
7

▶ Example

▶ Try it Yourself
TEXT
 10 20 30 40
 50 60 70 80
apple banana cherry
⚠️ Note: Mnemonic: 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

C
int (*pf)(int, int);

This declares pf as a pointer to a function that takes two ints and returns an int.

C
// 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;
}
TEXT
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.

C
// 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;
}
TEXT
2 4 6 8
-2 -4 -6 -8

▶ Example

▶ Try it Yourself
TEXT
3 7 19 42 55
55 42 19 7 3
🔥 Common Mistake: The standard library's 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

C
const int *p;
int const *p;

You cannot modify the pointed-to value through p, but p itself can point elsewhere.

C
// 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);
TEXT
10
20

(2) const Pointer

C
int * const p = &a;

p cannot change what it points to, but you can modify the value through p.

C
// Variable declaration
int a = 10;
int * const p = &a;
*p = 99;
// I/O operation
printf("%d\n", a);
TEXT
99

(3) const Pointer to const

C
const int * const p = &a;

Neither the pointer's target nor the value it points to can be changed — the strictest read-only.

💡 Tip: Quick trick: look at whether 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.

C
// 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);
TEXT
42
3.14

Typical uses of void *:

⚠️ Note: You cannot dereference a 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

输出:

TEXT
a=2, b=5

Reference entries in this section: qsort const malloc struct

❓ FAQ

Q How do function pointers in C work?
A A function pointer stores the address of a function. Declare with 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.
Q What does dereferencing a pointer mean in C?
A Dereferencing a pointer means going to the address it holds and reading or writing the value stored there. If 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.
Q What is array-to-pointer conversion aka. decay?
A When you pass an array as a function argument, it automatically becomes a pointer to its first element. That is why sizeof inside a function returns 8 (pointer size) instead of the array size. This is fundamental to how pointers and arrays work together in C.
Q How many levels of pointers can we have?
A Technically unlimited — C does not impose a cap. In practice, code becomes unreadable beyond three levels. A triple pointer (three stars) is the deepest you will see in real code, and even that is rare. Most pointer-heavy C code uses one or two levels at most.
Q Why use pointers?
A Pointers let you access the same memory from multiple places (aliasing), return multiple values from functions (out parameters), build dynamic data structures (linked lists, trees), and represent optional values (NULL). Without pointers, C would be a very different language.
Q Can you perform pointer arithmetic on a void pointer?
A No. The compiler does not know the size of the data a void pointer references, so the stride of p+1 is undefined. You must first cast it to a concrete type like unsigned char * or int * before doing arithmetic.
Q Why do we typedef struct so often in C?
A Because in C you must write 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

📝 Exercises

  1. 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
  2. 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
  3. 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
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏