c语言中初始化一个数组
In this article, we’ll take a look at how we will initialize an array in C.
在本文中,我们将研究如何在C中初始化数组。
There are different ways through which we can do this, so we’ll list them all one by one. Let’s get started!
我们可以通过多种方式来做到这一点,因此我们将它们一一列出。 让我们开始吧!
方法1:使用初始化列表初始化数组 (Method 1: Initialize an array using an Initializer List)
An initializer list initializes elements of an array in the order of the list.
初始化列表按列表的顺序初始化数组的元素。
For example, consider the below snippet:
例如,考虑以下代码片段:
int arr[5] = {1, 2, 3, 4, 5};
This initializes an array of size 5, with the elements {1, 2, 3, 4, 5} in order.
这将初始化一个大小为5的数组,其中元素{1、2、3、4、5}顺序排列。
This means that arr[0] = 1
, arr[1] = 2
, and so on.
这意味着arr[0] = 1
, arr[1] = 2
,依此类推。
We don’t need to initialize all the elements 0 to 4. We can even do only from indices 0 to 2.
我们不需要初始化所有元素0到4。我们甚至只能对索引0到2进行初始化。
The following code is also valid:
以下代码也是有效的:
int arr[5] = {1, 2, 3};
But now, arr[4]
and arr[5]
will still remain garbage values, so you need to be careful!
但是现在, arr[4]
和arr[5]
仍将是垃圾值,因此您需要小心!
If you’re using an initializer list with all elements, you don’t need to mention the size of the array.
如果您正在使用包含所有元素的初始化列表,则无需提及数组的大小。
// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};
If you want to initialize all the elements to 0, there is a shortcut for this (Only for 0). We can simply mention the index as 0.
如果要将所有元素初始化为0,则有一个快捷方式(仅适用于0)。 我们可以简单地将索引称为0。
#include <stdio.h>
int main() {
// You must mention the size of the array, if you want more than one
// element initialized to 0
// Here, all 5 elements are set to 0!
int arr[5] = {0};
for (int i=0; i<5; i++) {
printf("%d\n", arr[i]);
}
return 0;
}
Output
输出量
0
0
0
0
0
If you’re using multi-dimensional arrays, you can still initialize them all in one block, since arrays are stored in a row-wise manner.
如果您使用的是多维数组,由于数组是以行方式存储的,因此仍然可以在一个块中将它们全部初始化。
#include <stdio.h>
int main() {
int arr[3][3] = {1,2,3,4,5,6,7,8,9};
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
printf("%d\n", arr[i][j]);
return 0;
}
Output
输出量
1
2
3
4
5
6
7
8
9
A similar method can also be used for other datatypes, like float
, char
, char*
, etc.
类似的方法也可以用于其他数据类型,例如float
, char
, char*
等。
#include <stdio.h>
int main() {
// Array of char* elements (C "strings")
char* arr[9] = { "Hello", [1 ... 7] = "JournalDev", "Hi" };
for (int i=0; i<9; i++)
printf("%s\n", arr[i]);
return 0;
}
Output
输出量
Hello
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
Hi
Remember, this method with [1 … 7] = “Journaldev” might not work with all compilers. I work on GCC in Linux.
请记住,这种[1…7] =“ Journaldev”的方法可能不适用于所有编译器。 我在Linux上开发GCC。
方法2:使用for循环在C中初始化数组 (Method 2: Initialize an array in C using a for loop)
We can also use the for
loop to set the elements of an array.
我们还可以使用for
循环来设置数组的元素。
#include <stdio.h>
int main() {
// Declare the array
int arr[5];
for (int i=0; i<5; i++)
arr[i] = i;
for (int i=0; i<5; i++)
printf("%d\n", arr[i]);
return 0;
}
Output
输出量
0
1
2
3
4
方法3:使用指定的初始化程序(仅对于gcc编译器) (Method 3: Using Designated Initializers (For gcc compiler only))
If you’re using gcc
as your C compiler, you can use designated initializers, to set a specific range of the array to the same value.
如果将gcc
用作C编译器,则可以使用指定的初始化程序将数组的特定范围设置为相同的值。
// Valid only for gcc based compilers
// Use a designated initializer on the range
int arr[9] = { [0 ... 8] = 10 };
Note that there is a space between the numbers and there are the three dots. Otherwise, the compiler may think that it is a decimal point and throw an error.
请注意,数字之间有一个空格,并且有三个点。 否则,编译器可能会认为这是一个小数点并抛出错误。
#include <stdio.h>
int main() {
int arr[9] = { [0 ... 8] = 10 };
for (int i=0; i<9; i++)
printf("%d\n", arr[i]);
return 0;
}
Output (for gcc only)
输出(仅适用于gcc)
10
10
10
10
10
10
10
10
10
We can also combine this with our old initializer list elements!
我们也可以将其与旧的初始化列表元素结合起来!
For example, I am setting only array index arr[0], arr[8]
as 0, while the others are designated initialized to 10!
例如,我仅将数组索引arr[0], arr[8]
为0,而将其他数组都初始化为10!
#include <stdio.h>
int main() {
int arr[9] = { 0, [1 ... 7] = 10, 0 };
for (int i=0; i<9; i++)
printf("%d\n", arr[i]);
return 0;
}
Output
输出量
0
10
10
10
10
10
10
10
0
结论 (Conclusion)
In this article, we learned how we could initialize a C array, using different methods.
在本文中,我们学习了如何使用不同的方法初始化C数组。
For similar articles, do go through our tutorial section on C programming!
对于类似的文章,请阅读我们有关C编程的教程部分 !
翻译自: https://www.journaldev.com/39020/initialize-an-array-in-c
c语言中初始化一个数组