0

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.

2
  • 1
    collect is a pointer to a "one-dimensional" array - you are trying to use it like a "two-dimensional" array Commented Sep 16, 2020 at 19:57
  • And I have no idea what you did in that second example, but randomly guessing definitely does not work in C Commented Sep 16, 2020 at 19:57

1 Answer 1

1

Given that collect is declared as a double *, it follows that collect[i] is a double. You cannot subscript that.

Perhaps you want collect[i * cols + j] instead.

Or if your compiler supports the variable-length array option, then perhaps you wanted to declare collect differently:

double (*collect)[cols] = calloc(rows * sizeof(*collect));

..., in which case collect[i][j] would do what you expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.