The first of your options doesn't return pointers as desired.
Say you have
typedef struct { ... } StructA;
typedef struct { ... } StructB;
The following is commonly used:
void myFunction(StructA **aptr, StructA **bptr) {
StructA *a = malloc(sizeof(StructA));
StructB *b = malloc(sizeof(StructB));
... do stuff with a and b ...
*aptr = a;
*bptr = b;
}
void caller(void) {
StructA *a;
StructB *b;
myFunction(&a, &b);
...
}
or
void myFunction(StructA **aptr, StructA **bptr) {
*aptr = malloc(sizeof(StructA));
*bptr = malloc(sizeof(StructB));
... do stuff with *aptr and *bptr ...
}
void caller(void) {
StructA *a;
StructB *b;
myFunction(&a, &b);
...
}
But yes, you could use a struct as a return value.
typedef struct {
StructA *a;
StructB *b;
} StructAB;
StructAB myFunction(void) {
StructA *a = malloc(sizeof(StructA));
StructB *b = malloc(sizeof(StructB));
... do stuff with a and b ...
return (StructAB){ .a = a, .b = b };
}
void caller(void) {
StructAB ab = myFunction();
StructA *a = ab.a;
StructB *b = ab.b;
...
}
It's a bit more complicated, but not extremely so.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…