Originally Posted by
ronin
Don't use it myself, but couldn't something like below be used or have I forgotten how the mechanism works? I'm curious.
Code:
float* xxxx(float a, float b, float c, float d)
{
float *tmp = malloc(sizeof(float) * 2);
There's nothing wrong with it, except that you might forget to free. Probably the most failure-tolerant way is the suggestion from whiteflags, to make a struct. This has the advantage that you can name what the parameters are for. For example, say you have function foo that returns the sum and the product. Then you could write the function like this
Code:
typedef struct FOO_RESULT FOO_RESULT;
struct FOO_RESULT {
float sum;
float product;
};
FOO_RESULT foo(float a, float b, float c, float d) {
float x = a + b;
float y = c * d;
return (FOO_RESULT) {
.sum = x,
.product = y,
};
}
The caller of the function can do so in a way that is "self-documenting" and with no chance of memory allocation violations
Code:
FOO_RESULT res = foo(1.2f, 3.4f, 5.6f, 7.8f);
printf("Results are: %.2f, %.2f\n", res.sum, res.product);