I need to do a dot product of two vectors and then display the result. From what I have learned, the best I can do with C++ is return the address of the resulting vector (the pointer). I am currently doing that with this code:
// Dot product.
double * dot(double u[3], double v[3]) {
double result[3];
result[0] = u[0] * v[0];
result[1] = u[1] * v[1];
result[2] = u[2] * v[2];
return result;
I then need to print the result, so I have another function that accepts the pointer:
void pvec(double * ptr) {
cout << "[" << *(ptr + 0) <<", " << *(ptr + 1) << ", " << *(ptr + 2) << "]\n";
}
This does not work. The resulting values are all garbage.
How can I make this work? Coming from python, I really feel like not being able to pass arrays through functions is like have no arms or legs, and I can't manage to play the pointer game right.