Problem: As you can see, I am outputting 'nc' twice. But both the values differ. The second time 'nc' is displayed, it actually shows the 'temp1' value, and temp1 actually shows garbage values. Any help?
Your functions are returning a pointer to an array which is local to the function. These local arrays are no longer in scope after the function returns to main.
You should declare the arrays within main then pass a pointer to these arrays to the functions. The functions then just modify the array passed to it. Example:
#include <iostream>
usingnamespace std;
float cam[] = {-1.1,-1.0,1.2};
// this function modifies the array arr that is passed to it
void neg3(float* v, float* arr)
{
for(int i=0;i<3;i++)
arr[i] = 0.0-(v[i]);
return;
}
int main()
{
float nc[3];// declare this array here, where it will remain in scope.
neg3(cam, nc);
cout<<" "<<nc[0]<<" "<<nc[1]<<" "<<nc[2]<<endl;
return 0;
}// end of main()