How to pass two-dimensional array of function in C ++

I am trying to pass an array (2d) to a function as a parameter. I have the code as follows:

int main()
 {
  float T[100][100];
  void set_T(float T[][]);
}


void set_T(float T1[][])
{


  for (int i =0 ; i<90;i++)
  {
      for(int j =0 ;j <90;j++)
      {
          T1[i][j] = 3;
      }
  }

}

I'm not sure how to pass an array to a function ... I get a lot of errors. Can anyone help.

+5
source share
3 answers

Just name it like this:

int main ()
{
    float T[100][100];
    set_T(T);
}

And as @suddnely_me said, the type T1in the function declaration should be float**.

-3
source

There are two problems here:

  • C does not support 2D arrays, only arrays of arrays or arrays of pointers to arrays, none of which are the same as a 2D array
  • C , ( , 0- , , , , )

, - , 2D- - . , :

void set_T(float (*T1)[100]) {
    ... do stuff with T1[i][j] ...
}

int main() {
    float T[100][100];
    set_T(T);
}

T 100 100 , set_T 100 . 'T' set_T, 0- .

, - :

void set_T(float **T1) {
    ... do stuff with T1[i][j] ...
}

int main() {
    float *T[100];
    float space[100*100];
    for (int i = 0; i < 100; i++)
        T[i] = space + i*100;
    set_T(T);
}

, , . , , set_T, .

, ++, C, C- - std::vector std::array - C 1D , (, , )

+8
  void set_T(float (&T)[100][100]);
+5

All Articles