C语言 初始化结构数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18921559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing array of structures
提问by zubergu
Here's initialization I just found in somebody else's question.
这是我刚刚在别人的问题中找到的初始化。
my_data data[]={
{ .name = "Peter" },
{ .name = "James" },
{ .name = "John" },
{ .name = "Mike" }
};
I never saw something like this before and can't find explanation how is .name possible to be correct.
What I'm looking for is how step by step this process goes.
我以前从未见过这样的事情,也找不到解释 .name 怎么可能是正确的。
我正在寻找的是这个过程是如何一步一步进行的。
It looks like it gets:
看起来它得到:
1) data;
2) *data;
3) (*data).name;
4) (*data).name="Peter";
Or am I totally wrong?
还是我完全错了?
采纳答案by Dave
There are only two syntaxes at play here.
这里只有两种语法在起作用。
Plain old array initialisation:
int x[] = {0, 0}; // x[0] = 0, x[1] = 0A designated initialiser. See the accepted answer to this question: How to initialize a struct in accordance with C programming language standards
The syntax is pretty self-explanatory though. You can initialise like this:
struct X { int a; int b; } struct X foo = { 0, 1 }; // a = 0, b = 1or to use any ordering,
struct X foo = { .b = 0, .a = 1 }; // a = 1, b = 0
普通的旧数组初始化:
int x[] = {0, 0}; // x[0] = 0, x[1] = 0一个指定的初始化程序。请参阅此问题的公认答案:如何根据 C 编程语言标准初始化结构
虽然语法是不言自明的。你可以这样初始化:
struct X { int a; int b; } struct X foo = { 0, 1 }; // a = 0, b = 1或使用任何顺序,
struct X foo = { .b = 0, .a = 1 }; // a = 1, b = 0
回答by Grijesh Chauhan
my_datais a struct with nameas a field and data[]is arry of structs, you are initializing each index. read following:
my_data是一个带有name作为字段的结构并且是一个结构体data[],您正在初始化每个索引。阅读以下内容:
5.20 Designated Initializers:
In a structure initializer, specify the name of a field to initialize with
.fieldname ='before the element value. For example, given the following structure,struct point { int x, y; };the following initialization
struct point p = { .y = yvalue, .x = xvalue };is equivalent to
struct point p = { xvalue, yvalue };Another syntax which has the same meaning, obsolete since GCC 2.5, is
fieldname:', as shown here:struct point p = { y: yvalue, x: xvalue };
5.20 指定初始化器:
在结构初始
.fieldname ='值设定项中,在元素值之前指定要初始化的字段的名称。例如,给定以下结构,struct point { int x, y; };下面的初始化
struct point p = { .y = yvalue, .x = xvalue };相当于
struct point p = { xvalue, yvalue };另一个具有相同含义的语法(自 GCC 2.5 起已过时)是
fieldname:',如下所示:struct point p = { y: yvalue, x: xvalue };
You can also write:
你也可以写:
my_data data[]={
{ .name = "Peter" },
{ .name = "James" },
{ .name = "John" },
{ .name = "Mike" }
};
as:
作为:
my_data data[]={
{ [0].name = "Peter" },
{ [1].name = "James" },
{ [2].name = "John" },
{ [3].name = "Mike" }
};
Second form may be convenient as you don't need to write in order for example above is equivalent to:
第二种形式可能很方便,因为您不需要按顺序编写,例如上面的等效于:
my_data data[]={
{ [3].name = "Mike" },
{ [1].name = "James" },
{ [0].name = "Peter" },
{ [2].name = "John" }
};
To understand array initialization read Strange initializer expression?
Additionally, you may also like to read @Shafik Yaghmour's answer for switch case: What is “…” in switch-case in C code
理解数组初始化阅读奇怪的初始化表达式?
此外,您可能还想阅读@Shafik Yaghmour对 switch case 的回答:What is "..." in switch-case in C code
回答by AnT
There's no "step-by-step" here. When initialization is performed with constant expressions, the process is essentially performed at compile time. Of course, if the array is declared as a local object, it is allocated locally and initialized at run-time, but that can be still thought of as a single-step process that cannot be meaningfully subdivided.
这里没有“一步一步”。当使用常量表达式执行初始化时,该过程本质上是在编译时执行的。当然,如果数组被声明为本地对象,它会在本地分配并在运行时初始化,但这仍然可以被认为是一个无法进行有意义细分的单步过程。
Designated initializers allow you to supply an initializer for a specific member of struct object (or a specific element of an array). All other members get zero-initialized. So, if my_datais declared as
指定初始值设定项允许您为结构对象的特定成员(或数组的特定元素)提供初始值设定项。所有其他成员都被零初始化。所以,如果my_data声明为
typedef struct my_data {
int a;
const char *name;
double x;
} my_data;
then your
那么你的
my_data data[]={
{ .name = "Peter" },
{ .name = "James" },
{ .name = "John" },
{ .name = "Mike" }
};
is simply a more compact form of
只是一种更紧凑的形式
my_data data[4]={
{ 0, "Peter", 0 },
{ 0, "James", 0 },
{ 0, "John", 0 },
{ 0, "Mike", 0 }
};
I hope you know what the latter does.
我希望你知道后者是做什么的。
回答by Yu Hao
It's called designated initializerwhich is introduced in C99. It's used to initialize structor arrays, in this example, struct.
在 C99 中引入的称为指定初始值设定项。它用于初始化struct或数组,在本例中为struct.
Given
给定的
struct point {
int x, y;
};
the following initialization
下面的初始化
struct point p = { .y = 2, .x = 1 };
is equivalent to the C89-style
相当于C89式
struct point p = { 1, 2 };
回答by dhein
This is quite simple:
my_datais a before defined structure type.
So you want to declare an my_data-array of some elements, as you would do with
这很简单:
my_data是一个预先定义的结构类型。所以你想声明一个my_data-array 的一些元素,就像你做的那样
char a[] = { 'a', 'b', 'c', 'd' };
So the array would have 4 elements and you initialise them as
所以数组将有 4 个元素,你将它们初始化为
a[0] = 'a', a[1] = 'b', a[1] = 'c', a[1] ='d';
This is called a designated initializer (as i remember right).
这称为指定初始值设定项(我记得没错)。
and it just indicates that data has to be of type my_datand has to be an array that needs to store so many my_data structures that there is a structure with each type member name Peter, James, John and Mike.
它只是表明数据必须是类型my_dat并且必须是一个需要存储如此多 my_data 结构的数组,以至于每个类型成员都有一个结构,名称为 Peter、James、John 和 Mike。
回答by John Bode
It's a designated initializer, introduced with the C99 standard; it allows you to initialize specific members of a struct or union object by name. my_datais obviously a typedef for a structtype that has a member nameof type char *or char [N].
它是一个指定的构造器,随 C99 标准引入;它允许您按名称初始化结构或联合对象的特定成员。 my_data显然是具有struct类型成员name的类型的 typedefchar *或char [N]。

