首页 > 编程知识 正文

字符数组初始化,c语言字符数组赋值

时间:2023-05-03 15:48:18 阅读:114547 作者:3255

用c语言初始化数组

在In this article,we’lltakealookathowwewillinitializeanarrayinc。

本文讨论如何在c中初始化数组。

therearedifferentwaysthroughwhichwecandothis,sowe’lllistthemallonebyone.let’sget started!

因为这可以通过几种方法来实现,所以把它们一个个列出来。 开始吧!

方法1 )使用初始化列表初始化数组(method 1: initializeanarrayusinganitializerlistinitializeselementsofanarrayints )

初始化列表按列表顺序初始化数组中的元素。

For example,consider the below snippet:

例如,考虑以下片段:

intarr [5]={ 1,2,3,4,5 }; This initializes an array of size 5,with the elements http://www.Sina.com/in order。

这将按照元素{1, 2, 3, 4, 5}的顺序初始化大小为5的数组。

This means that arr[0]=1,arr[1]=2,and so on。

这意味着arr[0]=1,arr[1]=2。

we don’tneedtoinitializealltheelements0to4. wecanevendoonlyfromindices0to 2。

不需要初始化所有元素0到4。 只能初始化索引0到2。

thefollowingcodeisalsovalid :

以下代码也有效。

intarr [5]={ 1,2,3 }; But now,arr [4] andarr [5] willstillremaingarbagevalues,so you need to be careful!

但是,现在arr[4]和arr[5]仍然是垃圾的值,所以请小心。

if you’reusinganinitializerlistwithallelements,youdon’tneedtomentionthesizeofthearray。

如果使用的是包含所有元素的初始化列表,则不需要提到数组的大小。

//valid.sizeofthearrayistakenasthenumberofelements//intheinitializerlist (5) intarr )={ 1,2,3,4,5 }; //Also valid. But here,sizeof ais3inta [ ]={ 1,2,3 }; ifyouwanttoinitializealltheelementsto 0,thereisashortcutforthis (only for0).We can simply mention the index as 0。

如果要将所有元素初始化为0,则有快捷方式。 仅适用于0。 我们可以很容易地把索引称为0。

# include stdio.hint main ((/youmustmentionthesizeofthearray,ifyouwantmorethanone//elementinitializedto0////here ized ) for(intI=0; i5; I ) {printf('%dn ',arr[i] );

} return 0;}

Output

输出量

00000

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("%dn", arr[i][j]); return 0;}

Output

输出量

123456789

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("%sn", arr[i]); return 0;}

Output

输出量

HelloJournalDevJournalDevJournalDevJournalDevJournalDevJournalDevJournalDevHi

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("%dn", arr[i]); return 0;}

Output

输出量

01234 方法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 rangeint 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("%dn", arr[i]); return 0;}

Output (for gcc only)

输出(仅适用于gcc)

101010101010101010

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("%dn", arr[i]); return 0;}

Output

输出量

0101010101010100

结论 (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语言中初始化一个数组

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。