在C结构数组初始化 [英] struct array initialization in C
本文介绍了在C结构数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code的一部分。我想仅初始化 ArrayList的[0]
为 ArrayList的[0] .X = 0
和 ArrayList的[0] .Y = 0
。我并不需要初始化结构阵列的其余部分。我该怎么办呢?谢谢你。
的#include<&stdio.h中GT;
例如结构{
INT X;
诠释Ÿ;
};
例如结构数组列表[40];INT主(INT ARGC,CHAR *的argv []){
的printf(%d个\\ n%d个\\ N,ArrayList的[0] .X,ArrayList的[0] .Y);
返回0;
}
解决方案
您可以初始化结构数组的任何特定元素。
例如:
结构ArrayList的例子[40] = {[0] = {0,0}}; //设置结构的第0个元素结构示例数组列表[40] = {[5] = {0,0}}; //设置结构的第6要素
这就是所谓指定初始化这曾经是一个GNU扩展C99之前将它改编并自C99还支持标准C。
Here is part of my code. I would like to initialize only the arraylist[0]
as arraylist[0].x = 0
and arraylist[0].y = 0
. I do not need to initialize the rest of the struct array. How can I do it? Thank you.
#include <stdio.h>
struct example {
int x;
int y;
};
struct example arraylist[40];
int main(int argc, char *argv[]){
printf("%d\n %d\n", arraylist[0].x, arraylist[0].y);
return 0;
}
解决方案
You can initialise any particular element of the struct array.
For example:
struct example arraylist[40] = { [0]={0,0}}; //sets 0th element of struct
struct example arraylist[40] = { [5]={0,0}}; //sets 6th element of struct
This is called Designated Initializers which used to be a GNU extension before C99 adapted it and is also supported in standard C since C99.
这篇关于在C结构数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文