# Thread: 2D array as function parameter

1. ## 2D array as function parameter

Hi
I was trying to calculate the determinant of a 2X2 matrix. but the problem is passing in a 2D array. I know you need to give number of columns but in that it only declares 1D array. Follwing should make more sense.
Code:
```double det(double M[][5],int i,int j)
{
double det = 0;
if(i == j == 2)
{
det = M[i-2][j-2] * M[i-1][j-1] - M[i-1][j-2] * M[i-2][j-1];
return det;
}
}```
I am calling this function by:
Code:
`calcdet = det(Matrix,2,2);`
where Matrix is 2D array with values.

Any help would be really appreciated.

2. Declare it as a 'double **' instead of a 'double [][]'

The syntax for accessing the elements is identical

I would also suggest returning -1 or something similar if the 'i==j==2' fails instead of letting the function terminate without a return (it will give junk back to the calling function)

3. thanks for the reply i tried changing that but now am not getting the right answer. its always giving me -1 (else condition answer). here is my function:

Code:
```double det(double **M,int i,int j)
{
double det = 0;
if(i == j == 2)
{
det = M[i-2][j-2] * M[i-1][j-1] - M[i-1][j-2] * M[i-2][j-1];
return det;
}
else
return -1;
}```
and my main:
Code:
```int main()
{
int matsize,i=0,j=0;
double calcdet=0;
double Matrix [MAXSIZE][MAXSIZE]={{0}};
FILE* file = fopen("matrix.txt","r");
fscanf(file,"%d",&matsize);
for (i=0;i<matsize;i++)
{
for (j=0;j<matsize;j++)
{
fscanf(file,"%lf",&Matrix[i][j]);
}
j = 0;
}
fclose(file);
calcdet = det(&Matrix,2,2);
printf("%lf\n",calcdet);

system("PAUSE");
return 0;
}```
I dont know what am doing wrong so if you can figure out my mistake i will be really thankful to you.

4. You very carefully avoided any mention of a problem. Fortunately I can guess that you think "i == j == 2" means something, when in fact it does not. (Well, it does; I forget whether == associates l-to-r or r-to-l; if it associates r-to-l, it means "i==1 && j==2", if it associates l-to-r, it means "false".)

Note also that if you declare the array as double ** in your function prototype, you will not be able to use [bracket][notation] to access elements.

5. okie i changed my if condition. Now if i cant use bracket notation then how can i access elements. initially i was trying to use M[][5] but that did not work either. any suggestions?

6. Well, M[][5] does work, assuming MAXSIZE == 5. If not, it won't work, and it's not supposed to. (And even if it is, you should say M[][MAXSIZE] anyway.)

Edit: Oh, and don't pass Matrix as &Matrix -- you had that right in the first post.

7. thank you very much tabstop. works perfectly. i guess the whole problem was "i==j==2". good observation because it was tricky
thank you again

8. Originally Posted by Kernel Sanders
Declare it as a 'double **' instead of a 'double [][]'
Tabstop hinted at it: This is actually incorrect - for arrays that are not dynamically allocated [1], using double[][SIZE] is the correct way. The double ** form assumes that the argument is an array of pointers [if we keep the rest of the code in the function the same], and that the first pointer points to an array of double.

A two dimensional array when passed to a function is not turned into an array of pointers to arrays of elements by the compiler. The compiler does the same thing for two dimensional arrays (or n-dimensional) as it does for single dimension arrays: It passes the address of the first element. In the case of n-dinensional arrays, all dimensions except the first one must be given so that the compiler can determine how to find each dimension from the index - because essentially, the array is just a flat piece of memory to the function being called.

[1] It doesn't HAVE to be dynamically allocated, but it does have to be an array of pointers to double that is passed in, and those pointers should point to a suitable array of doubles.

--
Mats