Propeller C Multiple return
Can anyone help with multiple returns from a function in C.
I am looking to pass one piece of data into a function and get two pieces of data returned.
I am looking to pass one piece of data into a function and get two pieces of data returned.
Comments
it would be to integer values a=1234 b=4567.
Don't completely understand , but I will give it a try.
1. Return a pointer which could be a pointer to a struct with multiple values.
2. Take a struct parameter and fill multiple values in that.
3. Take multiple pointer parameters and fill values for them.
4. Variations of 1 through 3.
I haven't used C much lately, but I thought this was something C and Spin had in common.
Since Propeller GCC is just C targeted to the Propeller all of the generic C aspects translate. This is one such case.
This works fine:
#include <stdio.h> typedef struct { int value1; int value2; } ValuePair; ValuePair foo(int a, int b) { ValuePair result; result.value1 = a; result.value2 = b; return result; } int main(void) { ValuePair x; x = foo(1, 2); printf("value1 %d\n", x.value1); printf("value2 %d\n", x.value2); return 0; }
I wouldn't recommend using this technique to return large structures but it is certainly okay for small ones like in this example.That would be my suggestion. Anytime I need to pass around multiple related values a structure pops straight to mind. Mike's packing method will work too and is compact but the structure is more clear in my opinion.
#include <stdio.h> void TestSub(int parm, int *retval1, int *retval2) { *retval1 = parm + 1234; *retval2 = parm + 5678; } int main(void) { int a, b; TestSub(0, &a, &b); printf("a = %d, b = %d\n", a, b); return 0; }