If you need to return a struct
from a function, you would normally return a pointer to the struct
instead.
如果需要从函数返回结构,通常会返回指向结构的指针。
If you then want to return an array of structs, is it recommended to:
如果您想返回结构数组,建议:
- return an array of structures (pointer to the first element)
- 返回一个结构数组(指向第一个元素的指针)
- or return an array of struct pointers?
- 或者返回一个struct指针数组?
I have drawn a diagram for the two options below:
我已经为下面的两个选项绘制了一个图表:
1:
1:
2:
2:
Given the following struct definition
给出以下结构定义
struct values {
int a;
int b;
};
here is some sample code for accessing the fields of the structs from the two options:
这里有一些示例代码,用于从两个选项访问结构的字段:
Option #1:
选项1:
struct values *vals = get_values1();
printf("%d, %d\n", values[0].a, values[1].b);
Option #2:
选项#2:
struct values **vals = get_values2();
printf("%d, %d\n", values[0]->a, values[1]->b);
4 个解决方案
#1
6
The only issue I see to not use version 1, is that it might be easier to identify the number of structures (returned) in the second version, as a NULL
pointer can be used as a stopper element, whereas in the first version it might not be possible to define a stopper element.
我看到不使用版本1的唯一问题是,在第二个版本中识别结构(返回)的数量可能更容易,因为NULL指针可以用作止动元素,而在第一个版本中它可能不可能定义止动元件。
#2
5
Unless you have a good reason to return a pointer to pointers (for example, if you want to modify the pointers themselves later on), I think a pointer to the struct is good enough. No need to trick around with double pointers.
除非你有充分的理由返回指针指针(例如,如果你想稍后修改指针),我认为指向结构的指针就足够了。无需用双指针欺骗。
#3
1
As an example for a beginners book on C, the question would clearly be satisfied better by option 1 (no indirection) as was pointed out by the answers so far. If there is more to the question however (e.g. which is the best way to pass around structs in a large framework) then I would certainly go for option 2. Why? structs are complex units of data and tend to become standalone entities which are allocated and passed around and disposed in differing ways - all code that handles them by direct access will need a rewrite if data handling becomes more complex.
作为关于C的初学者书的一个例子,到目前为止答案中指出的选项1(没有间接)显然更好地满足了这个问题。如果问题还有更多(例如哪个是在大型框架中传递结构的最佳方法)那么我肯定会选择选项2.为什么?结构是复杂的数据单元,往往成为独立的实体,它们被分配和传递并以不同的方式处理 - 如果数据处理变得更加复杂,那么通过直接访问处理它们的所有代码都需要重写。
#4
0
For me, as the number of indirections grow, the code complexity grows even faster. Thus I prefer your Option #1: struct values *foo()
.
对我来说,随着间接数量的增加,代码复杂性变得更快。因此,我更喜欢你的选项#1:struct values * foo()。
Should data requirements push toward #2: struct values **foo()
, suggest creating a new type typedef struct values *ValuesSet
and returning a pointer to that: ValuesSet *foo()
.
如果数据要求推向#2:struct values ** foo(),建议创建一个新类型typedef struct values * ValuesSet并返回指向它的指针:ValuesSet * foo()。
#1
6
The only issue I see to not use version 1, is that it might be easier to identify the number of structures (returned) in the second version, as a NULL
pointer can be used as a stopper element, whereas in the first version it might not be possible to define a stopper element.
我看到不使用版本1的唯一问题是,在第二个版本中识别结构(返回)的数量可能更容易,因为NULL指针可以用作止动元素,而在第一个版本中它可能不可能定义止动元件。
#2
5
Unless you have a good reason to return a pointer to pointers (for example, if you want to modify the pointers themselves later on), I think a pointer to the struct is good enough. No need to trick around with double pointers.
除非你有充分的理由返回指针指针(例如,如果你想稍后修改指针),我认为指向结构的指针就足够了。无需用双指针欺骗。
#3
1
As an example for a beginners book on C, the question would clearly be satisfied better by option 1 (no indirection) as was pointed out by the answers so far. If there is more to the question however (e.g. which is the best way to pass around structs in a large framework) then I would certainly go for option 2. Why? structs are complex units of data and tend to become standalone entities which are allocated and passed around and disposed in differing ways - all code that handles them by direct access will need a rewrite if data handling becomes more complex.
作为关于C的初学者书的一个例子,到目前为止答案中指出的选项1(没有间接)显然更好地满足了这个问题。如果问题还有更多(例如哪个是在大型框架中传递结构的最佳方法)那么我肯定会选择选项2.为什么?结构是复杂的数据单元,往往成为独立的实体,它们被分配和传递并以不同的方式处理 - 如果数据处理变得更加复杂,那么通过直接访问处理它们的所有代码都需要重写。
#4
0
For me, as the number of indirections grow, the code complexity grows even faster. Thus I prefer your Option #1: struct values *foo()
.
对我来说,随着间接数量的增加,代码复杂性变得更快。因此,我更喜欢你的选项#1:struct values * foo()。
Should data requirements push toward #2: struct values **foo()
, suggest creating a new type typedef struct values *ValuesSet
and returning a pointer to that: ValuesSet *foo()
.
如果数据要求推向#2:struct values ** foo(),建议创建一个新类型typedef struct values * ValuesSet并返回指向它的指针:ValuesSet * foo()。