passing multidimensional arrays to function when the dimension size is not clear

前端 未结 2 1171
眼角桃花
眼角桃花 2021-01-28 00:09

I have a function in my class which should get a multi-dimensional array. the problem is that the value of these dimensions is calculated in the class, using another function an

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 01:02

    double calcSS(double tfpairexp[][max_ctrl_no][max_rep_no])
    

    One solution is to change the function signature to,

    double calcSS(double ***tfpairexp, int M, int N, int P);
    

    where M, N and P are dimensions of the arrray!

    Or you can pass the instance of your class itself. After all, you've made the dimensions public in your class. And if this function is a member function of the class, then you don't even need to pass anything; you can access them from the function itself like this:

    double calcSS(double ***tfpairexp)
    {
    
          for(int i = 0 ; i < cchips ; i++ )
          {
               for(int j = 0 ; j < max_ctrl_no ; j++ )
              {
                    for(int k = 0 ; k < max_rep_no ; k++ )
                    {
                           //access elements as tfpairexp[i][j][k]
                    }
              }
          }
    
    }
    

    You can see this answer if it solves your problem: using a function template

提交回复
热议问题