Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a two dimensional array to a function in c++

Tags:

c++

I am trying to pass an array (2d) to a function as an parameter. I have a 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 am not sure how to pass array to a function ...I am getting lot of errors. Can any one help please.

like image 826
Csharp_learner Avatar asked Apr 02 '12 00:04

Csharp_learner


People also ask

How do you pass one-dimensional and two-dimensional arrays to a function?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

How do you pass a two-dimensional character array to a function in C++?

We can have a pointer to the two-dimensional arrays in C++. We can pass a 2D array to a function by specifying the size of columns of a 2D array. We can also pass a pointer to an array. We can also define a pointer as a pointer to pass in a function.


2 Answers

There are two issues here:

  • C does not support 2D arrays, only arrays of arrays or arrays of pointers to arrays, neither of which is quite the same thing as a 2D array
  • C does not allow passing arrays to functions as arguments, only pointers into arrays (generaly, you use a pointer to an array's 0th element, since that's what the array's name ends up being so indexing off of such a pointer looks just like an array access)

So because of the first problem, you have to decide how you're going to represent a 2D array -- either an array of arrays, or an array of pointers to arrays. If you go the first route, your code ends up looking like:

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

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

Here, you've declared T to be an array of 100 arrays of 100 floats, and set_T takes a pointer to arrays of 100 floats as its argument. You pass 'T' directly to set_T, as the language treats array names as pointers to their 0th element.

If instead you want to use an array of pointers to arrays, you end up with something like:

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);
}

The disadvantage here is that you need to allocate space for all of the second-level arrays and manually initialize all the first-level pointers to point at them. The advangtage is that the sizes of the second level arrays is not part of the type of the argument passed to set_T, so you can more easily deal with variable-sized arrays.

Of course, if you're really using C++ and not C, you should not be using C arrays at all -- you should be using std::vector or std::array instead -- both of which share the C array 1D only issue, so you need a vector of vectors or an array of arrays (or conceivably a vector of arrays or an array of vectors)

like image 138
Chris Dodd Avatar answered Oct 07 '22 03:10

Chris Dodd


  void set_T(float (&T)[100][100]);
like image 24
Anycorn Avatar answered Oct 07 '22 04:10

Anycorn