简体   繁体   English

在没有赋值的情况下初始化struct?

[英]Initialize struct without an assignment?

I could not find an answer to this on the Internet, so here is my question: Can I define a struct instance without assigning it to a local or global variable in C? 我无法在互联网上找到答案,所以这是我的问题:我可以定义一个结构实例而不将其分配给C中的本地或全局变量吗? Eg: 例如:

struct A {
  int b;
}

struct A foo() {
  return struct A { .b = 42 };
}

If this is not possible: why? 如果这不可能:为什么?

Yes, you can use compound literals in C99 and later. 是的,您可以在C99及更高版本中使用复合文字。

return (struct A) { .b = 42 };

You can even point to them: 你甚至可以指向他们:

struct A *a = &(struct A) { .b = 42 };
a->b = 43;

These literals are "better" than string literals in that they are writable. 这些文字比字符串文字“更好”,因为它们是可写的。 The compiler may pool them if and only if you include const in the literal's type . 当且仅当在文字类型中包含const ,编译器才可以对它们进行汇集。

Yes C99 provides compound literals for this ( see it live ): 是的C99为此提供了复合文字( 请参见实时 ):

return (struct A) {  42 } ;

which is covered in the draft C99 standard section 6.5.2.5 Compound literals and says: C99标准6.5.2.5草案中涵盖了复合文字,并说:

A postfix expression that consists of a parenthesized type name followed by a brace enclosed list of initializers is a compound literal. 后缀表达式由带括号的类型名称后跟括号括起的初始值设定项列表组成,是一个复合文字。 It provides an unnamed object whose value is given by the initializer list. 它提供了一个未命名的对象,其值由初始化列表给出。 84) 84)

and: 和:

The value of the compound literal is that of an unnamed object initialized by the initializer list. 复合文字的值是初始化列表初始化的未命名对象的值。 If the compound literal occurs outside the body of a function, the object has static storage duration; 如果复合文字出现在函数体外,则该对象具有静态存储持续时间; otherwise, it has automatic storage duration associated with the enclosing block. 否则,它具有与封闭块相关的自动存储持续时间。

and provides several examples including: 并提供了几个例子,包括:

EXAMPLE 3 Initializers with designations can be combined with compound literals. 示例3具有指定的初始化器可以与复合文字组合。 Structure objects created using compound literals can be passed to functions without depending on member order: 使用复合文字创建的结构对象可以传递给函数,而不依赖于成员顺序:

drawline((struct point){.x=1, .y=1}, (struct point){.x=3, .y=4});

gcc also has a nice document on this in it's extension section since it supports this feature outside of C99 as well as clang . gcc在它的扩展部分也有一个很好的文档 ,因为它支持C99以外的这个功能以及clang

Yes, it is possible since C99. 是的,从C99开始就有可能。 That is a compound literal. 这是复合文字。

Still, yours has the wrong syntax. 你的语法仍然错误。 Use: 使用:

(struct A){.b=42}

or 要么

(struct A){42}

Though, go for constant literals if it does not matter: 但是,如果无关紧要,请选择恒定的文字:

(const struct A){.b=42}

All constant literals are subject to constant pooling (including string literals which have type char[] for historical reasons). 所有常量文字都受常量池的影响(包括由于历史原因而具有char[]类型的字符串文字)。
Constant compound literals and compound literals outside any function have static storage duration, 任何函数外的常量复合文字和复合文字都有静态存储持续时间,
the rest have automatic storage duration (beware returning a pointer, also they must be initialised each time). 其余的都有自动存储持续时间(注意返回一个指针,每次都必须初始化)。

In summary, prefer constant literals where possible. 总之,尽可能选择常量文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM