10

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:

  1. return an array of structures (pointer to the first element)
  2. or return an array of struct pointers?

I have drawn a diagram for the two options below:

1:

enter image description here

2:

enter image description here

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:

 struct values *vals = get_values1();
 printf("%d, %d\n", values[0].a, values[1].b);

Option #2:

 struct values **vals = get_values2();
 printf("%d, %d\n", values[0]->a, values[1]->b);
Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • @GrijeshChauhan what is best? Does it even matter which I choose? – Tyilo Aug 01 '13 at 11:19
  • 2
    The only issue I see to not use version 1, is that it might be easier to identify the number of structures 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 this stopper value. – alk Aug 01 '13 at 11:21
  • @alk, yeah you're right about that :) – Tyilo Aug 01 '13 at 11:23

4 Answers4

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.

alk
  • 69,737
  • 10
  • 105
  • 255
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.

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.

slartibartfast
  • 641
  • 4
  • 12
0

For me, as the number of indirections grow, the code complexity grows even faster. Thus I prefer your Option #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().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256