Reputation: 23
I'm new in C++ and I'm trying to learn about the arrays' behavior in functions. It's an attempt of matrix-vector multiplication.
#include <stdio.h>
#define N 4
float Gi[N][N] = {{ 0.0, 0.0, 0.0, 1.0},
{ 0.0, 2.57142857, 3.42857143, 1.28571429},
{-0.0, 3.42857143, 8.57142857, 1.71428571},
{ 1.0, 1.28571429, 1.71428571, 0.14285714}};
void mult(
float vec_I[N],
float vec_V[N]){
int acc;
for(int i = 0; i<N; i++){
acc = 0;
for(int j = 0; j<N; j++){
acc += Gi[i][j]*vec_I[j];
}
vec_V[i] = acc;
}
}
float solver(){
float out[N];
float I[N] = {0.0, 0.0, 0.0, 10.0};
float V[N] = {0.0, 0.0, 0.0, 0.0};
mult(I, V);
out[0] = V[0];
out[1] = V[1];
out[2] = V[2];
out[3] = V[3];
return out;
}
int main(){
float outPrint[4];
outPrint = solver();
printf("| %d |\n", outPrint[0]);
printf("| %d |\n", outPrint[1]);
printf("| %d |\n", outPrint[2]);
printf("| %d |\n", outPrint[3]);
return 0;
}
When I try to compile, the compiler tells me "[Error] cannot convert 'float*' to 'float' in return", about the return of the solver function (line 34). I can't understand why.
Upvotes: 0
Views: 151
Reputation: 635
Arrays are not first-class citizens. As such they are not returnable by value. You will have to make use of a struct wrapper as such:
struct matrix_t
{
float data[16];
};
Upvotes: 1