1

I need to return the value of the matrix, but I am gettin this error

Subscripted value is not an array, pointer, or vector

in:

qk_output[m][o] = 0;

and

qk_output[m][o] += queries[m][n] * keys[n][o];

Could anyone help me? Thanks!

int* multmm(int queries[M][N], int keys[N][O]) {
  // Matrix Multiplication
  int* qk_output = (int*)malloc(sizeof(int) *M*N);
  for (int m = 0; m < M; m++) {

    for (int o = 0; o < O; o ++) {
      qk_output[m][o] = 0;

      for (int n = 0; n < N; n++) {
        qk_output[m][o] += queries[m][n] * keys[n][o];
      }
    }
  }
 return qk_output;
}
4
  • what are the values of M N O? Commented Oct 30, 2021 at 5:47
  • qk_output is not a 2D array. qk_output is an int*. Thus qk_output[m] is an int, and qk_output[m][o] is invalid. Commented Oct 30, 2021 at 5:48
  • If you are going to use a flat 1D array and simulate a 2D array, you need to change qk_output[m][o] to qk_output[m * N + o] Commented Oct 30, 2021 at 6:18
  • The values are specific in the library Commented Oct 30, 2021 at 6:58

2 Answers 2

1

To return a 2D array from a function, you can do

#define N 4
#define M 3

int (*foo(void ))[N]
{
    int (*p)[N] = malloc(M * sizeof *p);
    for (int i = 0; i < M; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            p[i][j] = i + j;
        }
    }
    return p;
}

int main (void){
    int (*mat)[N] = foo();
    
    for (int i = 0; i < M; ++i)
    {
        for (int j = 0; j < N; ++j)
        {
            printf("%d ", mat[i][j]);
        }
        puts("");
    }
    
    free(mat);
    return 0;   
} 
Sign up to request clarification or add additional context in comments.

Comments

1
int* qk_output = (int*)malloc(sizeof(int) *M*N);

qk_output is a pointer to an int

the compiler knows how to access qk_output[n] but doesn't know how to access qk_output[m][n], you need to switch to:

int (*qk_output)[N] = malloc(sizeof(*qk_output) * M); // Don't cast malloc

that is, a pointer to an array of N ints

Now the compiler has enough information to access qk_output[m][n]

For the return question: you can use void *multmm(...) or int (*multmm(...))[N], the second one gives more information to the compiler so it is less error prone.

Something like:

int (*multmm(int queries[M][N], int keys[N][O]))[N]
{
    int (*qk_output)[N] = malloc(sizeof(*qk_output) * M);
    ...
    return qk_output;
}

int main(void)
{
    int (*arr2D)[N] = multmm(...);
    ...
}

2 Comments

Thank you very much but, I still have a similar error, in the malloc line copying the same code you mentioned here. Error: ERROR: [HLS 207-2754] cannot initialize a variable of type 'int (*)[2]' with an rvalue of type 'void *' (I have tried to do the casting in the malloc also and I get the same error)
It should workd: ideone.com/VlDTzy Try to extract a short fragment and it will be easier to help.

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.