C Array Initialization Methods
In C language, there are several ways to assign initial values to an array:
- Initialize the elements of an array at the same time as declaring the array, for example:
int arr[5] = {1, 2, 3, 4, 5};
- Assigning values to array elements using a loop structure, for example:
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
- Initialize the array elements with the memset function, for example:
int arr[5];
memset(arr, 0, sizeof(arr)); // 将数组元素全部赋值为0