[英]Is is possible to cast a simple block of code to a struct?
Consider this code : 考虑以下代码:
#include <stdio.h>
int main()
{
struct test {
int i;
int t;
};
struct test hello = (struct test) {
.i = 0,
.t = 1
};
printf("%d, %d\n", hello.i, hello.t);
return 0;
}
Output: 输出:
0, 1
my question is what is this line (struct test) {.i = 0, .t = 1 }
doing? 我的问题是这一行
(struct test) {.i = 0, .t = 1 }
在做什么?
is it casting a block of code to type struct test? 它是否在输入一个代码块来输入struct test? is that even possible?
甚至可能吗?
if not please tell me what is it doing, and thanks. 如果没有请告诉我它在做什么,谢谢。
struct test hello = (struct test) { .i = 0, .t = 1 };
uses two features of C called compound literals and designated initializers . 使用C的两个特征,称为复合文字和指定的初始化器 。
The general form of a compound literal is: (
type-name )
{
initializer-list }
. 复合文字的一般形式是:
(
type-name )
{
initializer-list }
。 (There may also be a comma after the list.) For example, these are compound literals: (列表后面可能还有一个逗号。)例如,这些是复合文字:
(int) { 3 }
(int []) { 0, 1, 2 }
(union { float f; unsigned int u; }) = { 3.4 }
A compound literal is an object with no name. 复合文字是没有名称的对象。
In a plain initializer list, you simply list values for the items in the object being initialized. 在普通的初始化列表中,您只需列出要初始化的对象中的项的值。 However, you can also use designated initializers.
但是,您也可以使用指定的初始化程序。 A designated initializer uses either the name of a structure member or the index of an array element to specify which part of the object is to be given the indicated value:
指定的初始值设定项使用结构成员的名称或数组元素的索引来指定对象的哪个部分被赋予指示值:
{ struct { int a, b, c; }) = { .b = 4, .c = 1, .a = 9 }
(int a[1024]) = { [473] = 1, [978] = -1 }
So, in struct test hello = (struct test) { .i = 0, .t = 1 };
所以,在
struct test hello = (struct test) { .i = 0, .t = 1 };
, we are creating struct test
with i
initialized to 0, and t
initialized to 1. Then this struct test
is used to initialize another struct test named hello
. ,我们创建
struct test
与i
初始化为0,并t
初始化为1。然后这个struct test
被用来初始化另一个名为结构测试hello
。
That particular use is pointless, since it creates a temporary object for the purpose of something that could have been done directly. 这种特殊用途毫无意义,因为它创造了一个临时对象,以达到可以直接完成的目的。 Nominally, it creates a temporary
struct test
which is initialized and then copied into the struct test hello
. 名义上,它创建一个临时的
struct test
,初始化然后复制到struct test hello
。 Then the temporary struct test
has no further use. 然后临时
struct test
没有进一步的使用。 The effect is the same as simply writing struct test hello = { .i = 0, .t = 1};
效果与简单编写
struct test hello = { .i = 0, .t = 1};
. 。 That initializes
hello
without using a temporary object. 在不使用临时对象的情况下初始化
hello
。 However, a good compiler will optimize them to the same code. 但是,一个好的编译器会将它们优化为相同的代码。
In C (and many related programming languages), the curly braces often mean: 在C(和许多相关的编程语言)中,花括号通常意味着:
{
this is where the code starts {
这是代码开始的地方
}
this is where the code ends }
这是那里的代码结束
They can also have several different meanings though. 它们也可以有几种不同的含义。 They don't only refer to code, but also to data structures:
它们不仅涉及代码,还涉及数据结构:
struct point {
int x;
int y;
};
In this case they define where the data structure begins and ends. 在这种情况下,它们定义数据结构的开始和结束位置。
struct point pythagoras = {
.x = 3,
.y = 4
};
And in this case they define where the initial contents of the variable begins and ends. 在这种情况下,它们定义变量的初始内容的开始和结束位置。
In conclusion, the {
means begin, and the }
means end. 总之,
{
表示开始,而}
表示结束。 Nothing more. 而已。
The "code" that initializes your struct test
is not really code, although it really looks like it. 初始化
struct test
的“代码”不是真正的代码,尽管它看起来真的很像。 Here it is again: 这又是:
struct test hello = (struct test) {
.i = 0,
.t = 1
};
Usually, the (type)
means a type cast. 通常,
(type)
表示类型转换。 But, in this case the (struct test)
is followed by an opening brace {
, and this combination is called a compound literal . 但是,在这种情况下,
(struct test)
后跟一个左括号{
,这个组合称为复合文字 。 It's a literal for a variable of a compound data type. 它是复合数据类型变量的文字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.