When I compile the following C code with Clang, I get this error: "Error: subscripted value is not an array, pointer, or vector" at the line collect[i][j] = result.
double * Matrix_Add(double *X, double *Y, double *input_length)
{
int rows = (int)input_length[0];
int cols = (int)input_length[1];
int output_size = rows * cols;
double *collect = (double *)calloc(rows * cols, sizeof(double));
double result;
int i = 0, j = 0;
// __________
while (i <= rows){
j = 1;
while (j <= cols){
result = X[i] + Y[j];
collect[i][j] = result;
j = j + 1; }
i = i + 1;
}
return 0;
}
So I added a int*
int i = 0, j = 0;
int * i_ptr = &i;
int * j_ptr = &j;
// __________
while (i <= rows){
j = 1;
while (j <= cols){
result = X[i] + Y[j];
collect[i_ptr][j_ptr] = result;
//collect[i][j] = result;
j = j + 1; }
i = i + 1;
}
but when I compile, I get "error: array subscript is not an integer."
I've read about a dozen Stack Overflow posts on this and I still don't know how to fix it.
Thanks.
collectis a pointer to a "one-dimensional" array - you are trying to use it like a "two-dimensional" array