If you need to return a struct from a function, you usually return a pointer to a struct .
If you want to return an array of structures, it is recommended:
- returns an array of structures (pointer to the first element)
- or return an array of pointers to a structure?
I drew a diagram for the following two options:
one

2:

Given the following definition of structure
struct values { int a; int b; };
Here is an example code for accessing the structure fields of two parameters:
Option number 1:
struct values *vals = get_values1(); printf("%d, %d\n", values[0].a, values[1].b);
Option number 2:
struct values **vals = get_values2(); printf("%d, %d\n", values[0]->a, values[1]->b);
c arrays pointers struct
Tyilo
source share