“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.
“Series of 100 Essential Notes for C Language Beginners“
Persevere! We are finally entering the practical series, which is also the most important and challenging part of C language.
Keep it up! Young learners!
[~~As a beginner, please forgive any mistakes~~]
54. When Does the Array Name in C Language Degenerate into a Pointer Type
Description
Arrays and pointers are two data types in C language, but they are closely related. One of the key points is that the array name automatically degenerates into a pointer to its first element in most cases.
However, there are two exceptional cases where the array name does not degenerate into a pointer. These distinctions can often be very confusing, and I would like to share them here.
Cases of Degeneration into Pointer
Function Parameter Passing:
void func(int arr[]); // Equivalent to void func(int* arr)
int main() {
int a[5];
func(a); // a degenerates to int*
return 0;
}
Assignment and Pointer Arithmetic:
int a[5];
int* p = a; // degeneration occurs
int* p2 = a + 1; // degeneration occurs
Exceptions Where Degeneration Does Not Occur
- 1. sizeof Operator
int a[5];
printf("%zu\n", sizeof(a)); // Outputs 20, not the size of a pointer
// Returns the byte size of the entire array
- 2. & Address-of Operator
int a[5];
int* p1 = a; // Pointer to the first element
int (*p2)[5] = &a; // Pointer to the entire array
printf("%p\n", p1); // Points to a[0]
printf("%p\n", p1 + 1); // Address +4 (advances by one int)
printf("%p\n", p2 + 1); // Address +20 (advances by the entire array)
Specific Application of Code
Getting the Number of Elements in an Array:
int a[10];
int count = sizeof(a) / sizeof(a[0]); // Correct: gets 10
The following function cannot get the size of the array:
void func(int arr[]) {
// Error! arr has degenerated to a pointer
int size = sizeof(arr) / sizeof(arr[0]);
}
// Correct approach
void func(int arr[], int size) {
// Use the passed size parameter
}
Summary
- • The array name usually automatically degenerates into a pointer type
- • It only maintains the array type during
<span>sizeof(arr)</span>and<span>&arr</span>
Have you understood? Although the syntax of C language is simple, there are still many issues that need to be accumulated and mastered gradually!
Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but I think having a group would indeed be convenient, so I will try to create one 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 for publication 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 mistakes, please bear with me~~]


“If you like C, please give a thumbs up”
Click the bottom right corner to view
“