Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have and function which calculate the
void threshold(float *output1,float *output2,int e, int y)
{

}
i want this two array n two integer to return to the main function .can anyone help?
Posted
Comments
nv3 20-Jan-15 2:54am    
The two arrays are being modified in place, as you just pass pointers. So with them you are done already. For the two integers: Just pass them by pointer as well:

void threshold (float* output1, float* output2, int* pe, int* py)

1 solution

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:
C#
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!

C++
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.

C#
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:
C++
void threshold(float *output1,float *output2,int e, int y, int *px, int *py)
{
myStruct ms;
...
*px = ms.X;
*py = ms.Y;
}
 
Share this answer
 
Comments
mybm1 22-Jan-15 23:31pm    
@OriginalGriff thanks dude but can u make it more clear how main function will call the function n where to declare the parameter.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900