You can only return a single value from any function, but that value can be a pointer. So if you "encapsulate" your two integers in a
struct
, then you can return a pointer to the struct and thus the multiple values.
But...be careful!
There are three ways to return the value:
myStruct* threshold(float *output1,float *output2,int e, int y)
{
myStruct ms;
...
return &ms;
}
This is bad, as the myStruct instance you create is allocated on teh stack, and the memory is deallocated when you exit the function. It's called a "hanging reference" and it can cause some horrible intermittent problems!
myStruct* threshold(float *output1,float *output2,int e, int y)
{
myStruct* pms = (myStruct*) malloc(sizeof(myStruct));
...
return pms;
}
This works, and doesn't cause code problems, but you need to have code to deallocate the memory, or your app will cause "memory leaks" and use more and more memory until it runs out and crashes.
myStruct ms;
myStruct* threshold(float *output1,float *output2,int e, int y)
{
...
return &ms;
}
This also works, but you need to be aware that there is only ever one instance of teh myStruct, so if you call threshold again, any previous integer values will be overwritten.
The better solution may be to pass pointers to two integers into the function, and modify those directly instead of returning a value:
void threshold(float *output1,float *output2,int e, int y, int *px, int *py)
{
myStruct ms;
...
*px = ms.X;
*py = ms.Y;
}