A Comprehensive Guide to Pointers in C Language and Their Uses

In the C language, pointers are one of the most fundamental, powerful, and easily confused concepts. They are not only tools for accessing memory but also play a crucial role in arrays, functions, structures, and many other areas. Below, I will guide you to fully understand the essence, types, uses, and examples of C language pointers.

1. What is a Pointer?

A pointer is a variable that stores the memory address of another variable.

Example:

int a = 10;
int* p = &a;
  • <span>a</span> is an integer variable with a value of <span>10</span>
  • <span>&a</span> is the address of <span>a</span>
  • <span>p</span> is a pointer to an int type, storing the address of <span>a</span>

2. Pointer-Related Symbols

Symbol Meaning
<span>*</span> Dereference (access the value pointed to by the pointer)
<span>&</span> Address-of operator (get the address of a variable)

3. Common Types of Pointers

1. Ordinary Pointer

int x = 5;
int* px = &x;
printf("%d\n", *px);  // Outputs 5

2. Null Pointer

int* p = NULL;  // Does not point to any valid memory

3. Pointer to Pointer (Multi-level Pointer)

int x = 10;
int* p = &x;
int** pp = &p;

printf("%d\n", **pp);  // Outputs 10

4. Pointer to an Array

int arr[3] = {1, 2, 3};
int* p = arr;

printf("%d\n", *(p + 1)); // Outputs 2

5. Pointer as Function Parameter (Pass by Reference)

void setZero(int* p) {
    *p = 0;
}

int x = 5;
setZero(&x);  // x becomes 0

6. Function Pointer

int add(int a, int b) {
    return a + b;
}

int (*fptr)(int, int) = add;
printf("%d\n", fptr(2, 3));  // Outputs 5

4. Uses and Advantages of Pointers

1. Pass by Reference (Simulating Reference Semantics)

Allows functions to modify external variables:

void swap(int* a, int* b) {
    int t = *a;
    *a = *b;
    *b = t;
}

2. Dynamic Memory Allocation

Must use pointers to manipulate heap space (<span>malloc/free</span>):

int* data = malloc(sizeof(int) * 10);
data[0] = 42;
free(data);

3. String and Array Traversal

A string is essentially a character pointer:

char* str = "Hello";
while (*str) {
    putchar(*str++);
}

4. Implementing Data Structures

Such as linked lists, trees, and graphs, all rely on pointers:

typedef struct Node {
    int val;
    struct Node* next;
} Node;

5. Function Pointers for Callbacks and Strategy Patterns

void apply(int a, int b, int (*op)(int, int)) {
    printf("Result = %d\n", op(a, b));
}

5. Common Pitfalls with Pointers

Problem Type Description
Null Pointer Dereference Using a <span>NULL</span> pointer will crash the program
Dangling Pointer The pointer points to a freed or uninitialized address
Memory Leak <span>malloc</span> without a corresponding <span>free</span>
Pointer Out of Bounds Array access out of range
Double Freeing the Same Address Can cause the program to crash

6. Example: Swapping Two Numbers

#include <stdio.h>

void swap(int* a, int* b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int main() {
    int x = 3, y = 7;
    swap(&x, &y);
    printf("x=%d, y=%d\n", x, y);  // x=7, y=3
    return 0;
}

7. Recommended Methods for Learning Pointers

  1. Start with basic definitions and usages (<span>int*</span>, <span>char*</span>, <span>void*</span>)
  2. Understand pointer arithmetic in conjunction with arrays and strings
  3. Write multiple examples of functions passing pointers
  4. Understand the combined use of malloc/free for dynamic allocation
  5. Read and understand linked list code, which is an advanced exercise in pointers

Leave a Comment