Scan the QR code to follow Chip Dynamics, and say goodbye to “chip” blockage!

Search WeChat
Chip Dynamics
Hello everyone! Today we are going to talk about that love-hate character in C language — pointers!
Some say that pointers are the soul of C language; if you haven’t learned pointers, you haven’t learned C.
Others say that pointers are the nightmare of C language; if you’re not careful, your program will crash before you know it!
Today, we will thoroughly understand pointers using humor and code, so you won’t be confused by their “pointing” anymore!
What is a pointer?1.1 Variable vs Pointer
Imagine a variable as your house, while a pointer is your house number.
-
Variable: Stores data (for example, int a = 10; a is the house, and it contains 10).
-
Pointer: Stores the memory address of a variable (for example, int *p = &a; p is the house number pointing to a’s house).
1.2 How to define a pointer?
The basic form of defining a pointer variable:
-
Base type *pointer variable name
int a = 10; // Define a normal variable
int *p = &a; // Define a pointer that points to the address of a
-
&a: Get the address of a (equivalent to asking: “Where is your house, a?”).
-
int *p: Declare p as a pointer to an int type.
When defining a pointer variable, pay attention to two points:
-
The * in front of the pointer variable indicates that the variable is of pointer type; the pointer variable name is p, not *p. This is different from the definition of integer and floating-point variables.
-
When defining a pointer variable, you must specify the base type. You might think that since a pointer variable stores an address, it only needs to be a pointer variable; why specify the base type? Different types of data occupy different bytes in memory. Therefore, it is necessary to specify the type of variable the pointer variable points to. A pointer variable can only point to variables of the same type and cannot point to an integer variable one moment and a floating-point variable the next.
1.3 Dereferencing a pointer
printf("Value of a: %d\n", a); // Directly access a, output 10
printf("Value pointed by p: %d\n", *p); // Access the value of a through *p, output 10
-
*p: Dereferencing, equivalent to “finding the house by house number p and checking who is inside”.
Pitfall Guide:
-
An uninitialized pointer is a wild pointer; pointing randomly can lead to program crashes!
int *p; // Error! p has not pointed anywhere, random pointing will cause issues!
*p = 10; // Crash warning!
-
Correct approach: Either point to a valid variable or initialize it to NULL.
int *p = NULL; // Safe approach, first let it point to "null"
Pointer assignment2.1 Pointer assignment
Pointers can change the target they point to:
int a = 10, b = 20;
int *p = &a; // p points to a
p = &b; // Now p points to b
printf("%d\n", *p); // Output 20
2.2 Pointer arithmetic
Pointers can perform addition and subtraction:
int arr[3] = {10, 20, 30};
int *p = arr; // p points to the first element of the array
printf("%d\n", *p); // Output 10
p++; // p now points to arr[1]
printf("%d\n", *p); // Output 20
Pitfall Guide:
-
Pointer arithmetic must ensure no out-of-bounds access; otherwise, accessing illegal memory will crash!
int arr[3] = {1, 2, 3};
int *p = &arr[2];
p++; // Dangerous! Already out of array bounds!
Pointers as function parameters3.1 Normal parameter passing vs pointer passing
-
Normal parameter passing: Modifications inside the function do not affect external variables (pass by value).
void change(int x) {
x = 100; // Only modifies the local variable
}
int main() {
int a = 10;
change(a);
printf("%d\n", a); // Still 10, unchanged!
}
-
Pointer parameter passing: The function can modify external variables (pass by address).
void change(int *p) {
*p = 100; // Modify external variable through pointer
}
int main() {
int a = 10;
change(&a);
printf("%d\n", a); // Now a becomes 100!
}
3.2 Classic case: Swapping the values of two variables
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x=%d, y=%d\n", x, y); // x=20, y=10
}
Pitfall Guide:
-
Don’t pass the wrong parameters! You must pass pointers to modify the original variables; passing values won’t work!
swap(x, y); // Error! Should pass addresses swap(&x, &y);
Common “crash” scenarios with pointers4.1 Dereferencing a null pointer (program crash)
int *p = NULL;
*p = 10; // Crash! p is a null pointer, cannot dereference!
4.2 Wild pointer (pointing to unknown memory)
int *p; // Uninitialized
*p = 10; // May crash, or may quietly modify memory that shouldn't be changed!
4.3 Pointer out of bounds (array access error)
int arr[3] = {1, 2, 3};
int *p = arr;
p += 5; // Out of bounds access, dangerous!
Summary
-
Pointers are the addresses of variables; & gets the address, * dereferences.
-
Pointers can modify external variables, suitable for function parameters.
-
Wild pointers, null pointers, and out-of-bounds access are common errors; be very careful!

If you find this article helpful, click “Share“, “Like“, or “View“!