简体   繁体   English

C中返回结构的函数

[英]Function returning struct in C

Where can we write code like我们在哪里可以编写代码

struct Foo
{
    int bar;
    int baz;
} foo()
{

}

C89/C90? C89/C90? C99? C99? C11? C11? Or maybe it's K&R only?或者它可能只是 K&R?

And what about this还有这个怎么办

void foo(bar, baz)
int bar;
int baz;
{
}

It's standard since C89.这是自 C89 以来的标准。 In K&R C, it was not possible to return structs, only pointers to structs.在 K&R C 中,无法返回结构,只能返回指向结构的指针。

It is possible and useful, certainly since C89, to return a struct .从 C89 开始,返回struct是可能且有用的。

Your second example foo is old K&R C and is deprecated in newer standards.您的第二个示例foo是旧的 K&R C,在较新的标准中已弃用。

Notice that on Linux x86-64 the ABI defines calling conventions which returns a two membered structure directly thru registers.请注意,在 Linux x86-64 上, ABI定义了直接通过寄存器返回两个成员结构的调用约定 Other structures are returned thru memory, so may be a little slower to return than a single pointer.其他结构通过内存返回,因此返回可能比单个指针慢一点。

For instance, you could define a point to be a struct of two numbers (eg int or double ) called x and y and return such a struct from some getPosition routine.例如,你可以定义一个点是一个struct两个数字(例如intdouble )被称为xy ,并返回这样的struct ,从一些getPosition程序。 On Linux/x86-64, the two numbers would be returned in two registers (without bothering building some struct in memory, when optimization is enabled and possible), eg:在 Linux/x86-64 上,这两个数字将在两个寄存器中返回(无需在内存中构建一些struct体,当启用优化并且可能时),例如:

 struct point_st { int x, y; };

 struct point_st getPosition(void);

 struct point_st getPosition () {
   struct point_st res = {-1,-1};
   res.x = input_x();
   res.y = input_y();
   return res;
 };

You generally want to declare the returned struct before the function's prototype.您通常希望在函数原型之前声明返回的struct

In C89/C90, it is possible to have a function that returns a struct.在 C89/C90 中,可以有一个返回结构体的函数。

I didn't find any explicit definition in the standard, but a footnote in section 3.3.2.3 "Structure and Union Members" says:我在标准中没有找到任何明确的定义,但第 3.3.2.3 节“结构和工会成员”中的脚注说:

EXAMPLE 1: If f is a function returning a structure or union, and x is a member of that structure or union, f().x is a valid postfix expression but is not an lvalue.示例 1:如果f是返回结构或联合的函数,而x是该结构或联合的成员,则f().x是有效的后缀表达式但不是左值。

Section 3.6.6.4 "The return Statement" doesn't lose a word about structures and unions as return types though.尽管如此,第 3.6.6.4 节“返回语句”并没有丢失关于​​结构和联合作为返回类型的内容。

Section 3.5.4.3 "Function declarators (including prototypes)" says:第 3.5.4.3 节“函数声明符(包括原型)”说:

A function declarator shall not specify a return type that is a function type or an array type.函数声明符不应指定函数类型或数组类型的返回类型。

It does not mention any restriction on structure or union types, and since this would be the place where such a restriction would belong, they are allowed.它没有提到对结构或联合类型的任何限制,因为这将是此类限制所属的地方,所以它们是被允许的。

C99 has the same wording, it just renumbered the sections. C99 有相同的措辞,只是重新编号了部分。

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

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