简体   繁体   English

返回结构的函数名

[英]Function name for returning a struct

I want to make a function in which I change an already existing struct. 我想创建一个函数,在其中更改一个已经存在的结构。 So the return value of that function should be a struct. 因此,该函数的返回值应为struct。 If I want an int as return value I call the function " int example() " ... How do I call the function if I want to return a struct? 如果我想要一个int作为返回值,则调用函数“ int example() ” ...如果我想返回一个结构,如何调用该函数? Since "struct" is already taken — I already take " struct whatever " to create one. 因为已经使用了“ struct whatever ”,所以我已经采用“ struct whatever ”来创建一个。

If you want the function to modify an existing struct, you should pass the struct in by pointer: 如果要让函数修改现有结构,则应通过指针传递该结构:

void modify_thing(struct whatever *thing);

If you want to return a modified copy of the struct, you can return the struct by value: 如果要返回该结构的修改后的副本 ,则可以按值返回该结构:

struct whatever edit_thing(const struct whatever *input);

Note that it is usually more efficient to pass struct variables by pointer, rather than by value. 注意,通过指针而不是通过值传递结构变量通常更有效。

passing a struct by value will cause the compiler to establish 1 or more reserved areas in memory that can only be used for that function. 按值传递结构将导致编译器在内存中建立1个或多个只能用于该功能的保留区。

Each of those memory areas are manipulated by the compiler inserting calls to memcpy(). 这些存储区中的每一个都由编译器插入对memcpy()的调用来操纵。

Much better to pass a pointer to the struct, then return a simple indication of success/failure of the struct update operation. 最好将一个指针传递给该结构,然后返回一个简单的指示结构更新操作成功/失败的指示。

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

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