5

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.

1

3 Answers 3

8

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)

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

1 Comment

I feel like your answer could list some better alternatives to float (*)[100] or float** to be truly complete.
6
  void set_T(float (&T)[100][100]);

Comments

-4

Just call it like this:

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

And as @suddnely_me said, the type of T1 in the function declaration need to be float**.

2 Comments

float[][] and float** are different.
@Jesse is right. A C-style 2D array is not implemented as an array of pointers to arrays. It's an array of arrays, i.e. first row of 100 elements followed by second row of 100 elements, etc. So to properly access a 2D array using C syntax, the called function needs all but one dimension to be specified.

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.