目次
1. Introduction
In C, “array initialization” is a very important concept for programming. In particular, by properly using bulk initialization, you can improve code readability and prevent bugs. In this article, we will explain in detail how to bulk-initialize arrays in C. We cover everything from basic array definitions to multidimensional array initialization, methods usingmemset
, and even initialization techniques in C++ such as std::fill
. Whether you are a beginner or an intermediate programmer, you will gain practical knowledge, so please read to the end.2. Basics of Arrays
2.1 What is an Array
An array is a data structure that stores multiple pieces of data of the same type in a contiguous memory region. For example, when you want to manage several integer (int
) variables together, you use an array.2.2 How to Declare an Array
In C, array declarations are done as follows.type array_name[size];
For example, to define an array that holds five integers, you write as follows.int numbers[5];
2.3 Memory Layout of Arrays and Accessing Elements
Array elements can be accessed using a zero-based index. Let’s look at the following code.#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("%dn", numbers[0]); // 10
printf("%dn", numbers[1]); // 20
printf("%dn", numbers[4]); // 50
return 0;
}
3. Array Initialization Methods
3.1 Individual Initialization
A method that initializes each array element by specifying them one at a time.int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
3.2 Bulk Initialization
A method that initializes by specifying values all at once during declaration.int numbers[5] = {10, 20, 30, 40, 50};
It is also possible to omit the array size.int numbers[] = {10, 20, 30, 40, 50};
In this case, the compiler automatically determines the size based on the number of elements in the initializer list.3.3 Partial Initialization
If only some elements are specified in the initializer list, the unspecified parts are automatically filled with zeros.int numbers[5] = {10, 20}; // The remaining elements become 0

4. Bulk Initialization with a Specific Value
4.1 Using the memset
Function
memset
is a function that fills memory byte by byte. It is handy, for example, when initializing an array entirely to 0
.#include <stdio.h>
#include <string.h>
int main() {
int numbers[5];
memset(numbers, 0, sizeof(numbers));
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // 0 0 0 0 0
}
return 0;
}
4.2 Initialization Using a for
Loop
Using a loop, you can initialize all elements of an array to any value.int numbers[5];
for (int i = 0; i < 5; i++) {
numbers[i] = 100; // set all elements to 100
}
4.3 Using std::fill
in C++
In C++, you can use std::fill
for more concise initialization.#include <iostream>
#include <algorithm>
int main() {
int numbers[5];
std::fill(numbers, numbers + 5, 100); // set all to 100
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " "; // 100 100 100 100 100
}
return 0;
}
5. Multidimensional Array Initialization
5.1 Declaration and Initialization of 2D Arrays
Basic Structure
int matrix[3][3];
Aggregate Initialization
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
5.2 Initialization of Multidimensional Arrays Using memset
#include <stdio.h>
#include <string.h>
int main() {
int matrix[3][3];
memset(matrix, 0, sizeof(matrix)); // Fill the entire array with zeros
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]); // All zeros are printed
}
printf("n");
}
return 0;
}
6. Initialization Tips and Best Practices
6.1 Bugs Caused by Uninitialized Arrays
In C, local variables (variables on the stack) are not initialized by default。#include <stdio.h>
int main() {
int numbers[5]; // Not initialized
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // May output indeterminate values
}
return 0;
}
Solution:int numbers[5] = {0}; // Initialize all elements to 0
6.2 memset
Scope
#include <stdio.h>
#include <string.h>
int main() {
int numbers[5];
memset(numbers, 0, sizeof(numbers)); // Initialize all to 0
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // 0 0 0 0 0
}
return 0;
}
6.3 Improving Readability and Maintainability
int matrix[3][3] = {
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
};
7. FAQ (Frequently Asked Questions)
7.1 Can you set an arbitrary value with memset
?
int numbers[5];
memset(numbers, 5, sizeof(numbers)); // This is wrong!
Correct methodfor (int i = 0; i < 5; i++) {
numbers[i] = 5;
}
7.2 Can you initialize a multidimensional array with memset
?
int matrix[3][3];
memset(matrix, 0, sizeof(matrix));
7.3 How can you bulk-initialize an array of structs?
struct Point {
int x;
int y;
};
struct Point points[3] = {
{1, 2},
{3, 4},
{5, 6}
};
8. Summary
8.1 Review of Key Points
- Do not use local variable arrays without initializing them!
- Ensure the size of the initializer list matches the array size
- Global and static variables are initialized to 0 by default
- Use
memset
carefully with integer arrays - Initializing large arrays with a
for
loop is appropriate
8.2 Code Example: Proper Initialization in Practice
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Bulk initialization
int arr1[5] = {1, 2, 3, 4, 5};
// Zero initialization using memset
int arr3[5];
memset(arr3, 0, sizeof(arr3));
// Initialization using a for loop
int arr4[5];
for (int i = 0; i < 5; i++) {
arr4[i] = i * 10;
}
// Display results
printf("arr1: ");
for (int i = 0; i < 5; i++) printf("%d ", arr1[i]);
printf("n");
printf("arr3: ");
for (int i = 0; i < 5; i++) printf("%d ", arr3[i]);
printf("n");
printf("arr4: ");
for (int i = 0; i < 5; i++) printf("%d ", arr4[i]);
printf("n");
return 0;
}