Returning multiple values from a C function
c, data-structures
Solution
As Neil says, you need to judge it for yourself.
To avoid the cost of passing anything, use a global. Next best is a single structure passed by pointer/reference. After that are individual pointer/reference params.
However, if you have to pack data into the structure and then read it back out after the call, you may be better off passing individual parameters.
If you're not sure, just write a bit of quick test code using both approaches, execute each a few hundred thousand times, and time them to see which is best.
Problem
Important: Please see this very much related question: Return multiple values in C++. I'm after how to do the same thing in ANSI C? Would you use a struct or pass the addresses of the params in the function? I'm after extremely efficient (fast) code (time and space), even at the cost of readability. EDIT: Thanks for all the answers. Ok, I think I owe some explanation: I'm writing this book about a certain subset of algorithms for a particular domain. I have set myself the quite arbitrary goal of making the most efficient (time and space) implementations for all my algos to put up on the web, at the cost of readability and other stuff. That is in part the nature of my (general) question. Answer: I hope I get this straight, from (possibly) fastest to more common-sensical (all of this a priori, i.e. without testing): - Store outvalues in global object (I would assume something like outvals[2]?), or - Pass outvalues as params in the function (foo(int in, int *out1, int *out2)), or - return a struct with both outvals, or - (3) only if the values are semantically related. Does this make sense? If so, I think Jason's response is the closest, even though they all provide some piece of the "puzzle". Robert's is fine, but at this time semantics is not what I'm after (although his advice is duly noted).