Hi, everyone! Is it safe to let a function return a structure type? For example, which function is better in practice in the example below?
Thanks in advance!
Code:
#include <stdio.h>
struct abc {
int i;
float f;
};
static struct abc f1 (int, float);
static void f2 (struct abc *, int, float);
struct abc f1 (int i, float f) {
struct abc a;
a.i = i;
a.f = f;
return a;
}
void f2 (struct abc *a, int i, float f) {
a->i = i;
a->f = f;
}
int main (void) {
int i = 10;
float f = 12.3;
struct abc a, b;
a = f1 (i, f);
f2 (&b, i, f);
printf("a: %3d %5.2f\n", a.i, a.f);
printf("b: %3d %5.2f\n", b.i, b.f);
return 0;
}