🎓 C Language Learning Column | Chapter 5: Arrays and Pointers – The Magical Combination of C Language
💬 “Arrays and pointers are like twin brothers, looking different but connected in their minds.”
Mastering them allows you to make C programs play like a piano in memory.
📌 Chapter Navigation
| Module | You Will Understand |
|---|---|
| The Essence of Arrays | A block of contiguous memory |
| What is the Array Name? | It is a ‘constant pointer’ |
| How Pointers Access Array Elements | The secret of p[i] == *(p+i) |
| Pointer Arithmetic | p++ does not add 1, but “jumps to the next element” |
| The Relationship Between Arrays and Functions | Why does it degrade to a pointer when passing parameters? |
1. The Truth About Arrays: A Block of Contiguous Memory
Let’s look at a very common example 👇
int arr[5] = {10, 20, 30, 40, 50};
You might think you created 5 independent variables,
but in memory, it actually looks like this:
| Address (Illustration) | Content |
|---|---|
| 0x1000 | 10 |
| 0x1004 | 20 |
| 0x1008 | 30 |
| 0x100C | 40 |
| 0x1010 | 50 |
📌 Each int occupies 4 bytes, tightly arranged with no gaps.
This is the “contiguous storage feature” of C language.
2. What is the Array Name?
When you write:
printf("%p\n", arr);
printf("%p\n", &arr[0]);
The output is almost the same.
✅ Because the array name arr is actually a “constant pointer” pointing to the first element!
In other words:
arr == &arr[0];
But note ⚠️:
•
arr is a constant and cannot be modified;
•
&arr[0] is an address value that can be assigned to a pointer.
int *p = arr; // ✅ OK, the array name automatically degrades to a pointer
int *p = &arr[0]; // equivalent to
3. Two Ways to Access Array Elements
1️⃣ Traditional Way
printf("%d", arr[2]); // Print the third element
2️⃣ Pointer Way
printf("%d", *(arr + 2)); // Equivalent writing
💡 Because arr[i] is actually translated by the compiler to:
*(arr + i)
So [] is just syntactic sugar 🍬
The actual execution is an “address offset + dereference” operation.
4. The Magic of Pointer Arithmetic
Let’s look at this code:
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d\n", *p); // Outputs 10
printf("%d\n", *(p+1)); // Outputs 20
printf("%d\n", *(p+2)); // Outputs 30
💡 When you do p+1, the pointer does not add 1 byte,
but jumps over one int type (usually 4 bytes).
| Operation | Address Change | Pointing Element |
|---|---|---|
| p | 0x1000 | 10 |
| p+1 | 0x1004 | 20 |
| p+2 | 0x1008 | 30 |
So the step size of pointer +1 depends on the type it points to.
This is the significance of the type system in pointers.
5. The Complete Equivalence of Pointers and Arrays
If you write:
int *p = arr;
Then the results of the following expressions are all the same 👇
| Expression | Meaning | Result |
|---|---|---|
| arr[0] | The first element | 10 |
| *arr | Dereference the first element | 10 |
| *(arr + 1) | The second element | 20 |
| p[1] | The second element of the pointer | 20 |
📌 Note: The array name and pointer can almost be interchanged, just the difference between “constant pointer vs normal pointer”.
6. Why Does Passing an Array Transform into a Pointer?
When you pass an array to a function, for example:
void print_array(int arr[], int n) {
for(int i=0; i<n; ",="" 2,="" 3);="" 3};=""
The function parameter int arr[] actually automatically degrades to int *arr.
In other words, the function receives an “address”, not the entire array.
So in C, “passing an array” is actually “passing a pointer”!
This saves space and allows for efficient operations.
7. Mind Map: The Relationship Between Arrays and Pointers 🧠
arr[0] arr[1] arr[2]
10 → 20 → 30
↑
|
p
•
p = arr;
•
(p+1) accesses the next element;
•
p[i] == *(p+i);
•
arr always points to the first block of memory and cannot be moved;
•
p can move freely (for example, p++).
8. The Magical Techniques of Arrays and Pointers
✅ Traversing an Array
for (int *p = arr; p < arr + 5; p++) {
printf("%d ", *p);
}
✅ Copying an Array (Simple Version)
for (int i = 0; i < 5; i++) {
*(copy+i) = *(arr+i);
}
✅ Reversing an Array
int *left = arr;
int *right = arr + 4;
while (left < right) {
int temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
💡 All these operations do not use indices, relying entirely on “address arithmetic”.
This is the embodiment of C language being “close to hardware”.
🧠 Chapter Summary
| Concept | Meaning |
|---|---|
| Array | A block of contiguous memory |
| Array Name | A constant pointer to the first element |
| Pointer Arithmetic | Offset forward/backward based on type |
| p[i] == *(p+i) | Completely equivalent |
| Passing an array actually passes a pointer | Saves space + improves efficiency |
📎 Practice Suggestions:
1.
Write a function that traverses an array using pointers and sums the elements.
2.
Try to swap the maximum and minimum values in the array using pointers.
3.
Implement print_array() without using indices, only using pointers.
✨ By now, you have mastered the skill of “navigating the memory map” in C language.
Next, we will combine pointers with characters –
to make your C programs truly “understand text”.
💡
If you enjoy this “hardcore yet gentle” learning pace, remember to bookmark the column, and see you in the next chapter!~