“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute every day to remember the basics of C language.
“C Language Beginner’s Essential Knowledge Points Note Series of 100 Articles”“
The following notes finally enter the practical series, which is also the most important and difficult part of C language. Keep it up! Young learners!
37. The Most Confusing Question for C Language Beginners: Understanding the Relationship Between Pointers and Arrays, Finally Understood!
1. The Relationship Between Pointers and Arrays
Pointers and arrays are inseparable in C language; the array name is usually converted to a pointer pointing to its first element:
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Equivalent to int *ptr = &arr[0]
Key Features
- •
<span>arr[i]</span>is equivalent to<span>*(arr + i)</span> - •
<span>&arr[i]</span>is equivalent to<span>arr + i</span> - • The array name is not an lvalue and cannot be assigned or modified, for example,
<span>arr = &addr</span>is incorrect
2. Pointer Arithmetic Rules
1. Basic Operation Types
| Operation | Effect | Example |
|---|---|---|
| ptr + n | Advance n elements (not bytes) | ptr + 2 |
| ptr – n | Retreat n elements | ptr – 1 |
| ptr++ | Point to the next element | while(*ptr++) |
| ptr– | Point to the previous element | while(*ptr–) |
| ptr1 – ptr2 | Calculate the number of elements between two pointers | int diff = ptr1 – ptr2 |
It is the difference in elements, not bytes; one element may occupy multiple bytes, pointer difference = element difference * element size.
2. Type Determines Step Size
char *cp; // cp+1 advances 1 byte
int *ip; // ip+1 advances sizeof(int) bytes
double *dp; // dp+1 advances sizeof(double) bytes
3. Pointer Access to Arrays
1. Accessing One-Dimensional Arrays
int arr[5] = {1,2,3,4,5};
for (int *p = arr; p < arr + 5; p++) {
printf("%d ", *p);
}
2. Accessing Two-Dimensional Arrays
int matrix[3][4] = {...};
// Using a single pointer to access a two-dimensional array
int *p = &matrix[0][0];
for (int i = 0; i < 3*4; i++) {
printf("%d ", *(p + i));
}
4. Differences Between Pointers and Arrays
| Feature | Pointer | Array |
|---|---|---|
| Memory Usage | Fixed size (usually 4/8 bytes) | Number of elements × size of elements |
| Stored Content | Memory address | Actual data |
| sizeof | Returns pointer size | Returns total byte size of the array |
| Modifiability | Can be reassigned | Cannot be reassigned |
| As Function Parameters | Pass pointer value | Degenerates to pointer |
5. Typical Applications of Pointer Arithmetic
1. Adding an Integer to a Pointer
int arr[5] = {0};
int *p = arr;
p = p + 2; // Equivalent to p = &arr[2]
2. Subtracting Pointers
int *p1 = &arr[1];
int *p2 = &arr[4];
int n = p2 - p1; // n = 3 (difference in number of elements)
3. Comparing Pointers
if (p1 < p2) { // Compare memory addresses
// The element pointed to by p1 is before p2
}
Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but considering that having a group is indeed convenient, I will try to set one up this time.
If you need it, hurry up and join; the validity period is 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author. Some content and images are sourced from the internet. Please feel free to use them. The views are for learning reference only. If there are any errors or omissions, please forgive me.]


“If you like C, please like it”
Click the bottom right corner to view
“