“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“Series of 100 Essential Knowledge Points for C Language Beginners“
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!
32. Summary of C Language Array Usage Techniques and Its Relationship with Pointers: Declaration and Usage of One-Dimensional Arrays, Arrays are the Most Common and Important Knowledge Points!
1. Basic Concept of Arrays
An array is one of the most important data structures in C language; it is a collection of elements of the same type stored in contiguous memory. The characteristics of arrays include:
- • Fixed size (determined at compile time)
- • Same element type
- • Contiguous memory allocation
- • Accessed by index (starting from 0)
2. Declaration of One-Dimensional Arrays
1. Basic Syntax
data_type array_name[array_length];
2. Declaration Examples
int scores[5]; // Array of 5 integers
double temps[30]; // Array of 30 double precision floats
char name[20]; // Array of 20 characters (string)
3. Initialization of Arrays
1. Complete Initialization
int primes[5] = {2, 3, 5, 7, 11};
2. Partial Initialization (remaining elements are automatically set to 0)
int arr[5] = {1, 2}; // [1, 2, 0, 0, 0]
3. Automatic Length Calculation
int days[] = {31, 28, 31, 30, 31}; // Automatically determined to have 5 elements
4. New designated initialization in C99 standard, not supported in C89
int arr[10] = {[3]=5, [7]=9}; // Others are 0
It is recommended that everyone use a C99 standard compiler and development tools in the future; do not use outdated standards and tools.
4. Accessing Array Elements
1. Index Access Syntax
array_name[index] // Index starts from 0
2. Access Example
int nums[3] = {10, 20, 30};
nums[0] = 15; // Modify the first element
int x = nums[2]; // Read the third element
3. Danger of Out-of-Bounds Access
int arr[3];
arr[5] = 10; // Undefined behavior! May corrupt other memory, causing program crash
5. Memory Layout of Arrays
1. Memory Address Calculation
int arr[3];
// Assume the base address of arr is 0x1000
// arr[0] is at 0x1000
// arr[1] is at 0x1004 (int is usually 4 bytes)
// arr[2] is at 0x1008
2. Verify Memory Address Calculation with a Program
int arr[3];
printf("arr: %p\n", (void*)arr);
printf("&arr[0]: %p\n", (void*)&arr[0]);
printf("&arr[1]: %p\n", (void*)&arr[1]);
6. Relationship Between Arrays and Pointers
1. Array Name is a Constant Pointer
int arr[5];
int *p = arr; // Equivalent to &arr[0]
2. Pointer Arithmetic
*(arr + 2) = 5; // Equivalent to arr[2] = 5
7. Common Operations on Arrays
1. Traversing an Array
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
2. Summing an Array
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
3. Finding the Maximum Value
int max = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
8. Arrays as Function Parameters
1. Passing an Array to a Function
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printArray(nums, 5);
}
2. Actual Passing is a Pointer
void modifyArray(int *arr, int size) {
arr[0] = 100; // Modification will affect the original array
}
9. Common Errors
1. Out-of-Bounds Access
int arr[3] = {1, 2, 3};
printf("%d", arr[5]); // Error!
Modify to restrict index access range:
if (index >= 0 && index < 3) {
printf("%d", arr[index]);
}
2. Uncertain Array Size
int n = 10;
int arr[n]; // This syntax was incorrect before C99 standard; supported after C99 for variable-length arrays
It is recommended to modify as follows:
#define SIZE 10
int arr[SIZE];
It is recommended not to use variable-length arrays, even in the latest C99 standard, as their implementation depends on the compiler, leading to poor portability. Additionally, they only support small arrays, not large arrays. If the array size is uncertain, use dynamic memory allocation.
Some students contacted me, wanting to have a study exchange group. Previously, I hesitated to create a group due to concerns about advertisements, but I think having a group would indeed be convenient, so I will create one to try it out.
If you need to join, please hurry, 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 give a thumbs up”
Click the bottom right corner to view
“