# How to pass 2D-array to funtcion

This is a discussion on How to pass 2D-array to funtcion within the C Programming forums, part of the General Programming Boards category; I don't know how to pass 2D-array to function... i try this code for 2D but error happend... #include <stdio.h> ...

1. ## How to pass 2D-array to funtcion

I don't know how to pass 2D-array to function...
i try this code for 2D but error happend...

#include <stdio.h>

int kira(const int nom[][])
{
int jumlah;
int i,j;
jumlah =0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
jumlah = jumlah + nom[i][j];
}
}
return jumlah;
}

main()
{
int total, nom[3][3];
int i,j;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
nom[i][j] = 1;

total = kira(nom);
printf("Jumlah = %d\n", total);
}

-------------------
the errors are
pass.c: In function `kira':
pass.c:12: arithmetic on pointer to an incomplete type
pass.c: In function `main':
pass.c:27: warning: passing arg 1 of `kira' from incompatible pointer type

I don't how to do..

thank you

2. >I don't know how to pass 2D-array to function...
You have it right except for one thing, when passing multidimensional arrays to a function, you must specify the size of all but the first dimension:
Code:
```#include <stdio.h>

int kira(const int nom[][3])
{
int jumlah;
int i,j;
jumlah =0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
jumlah = jumlah + nom[i][j];
}
}
return jumlah;
}

int main(void)
{
int total, nom[3][3];
int i,j;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
nom[i][j] = 1;

total = kira(nom);
printf("Jumlah = %d\n", total);
return 0;
}```
-Prelude

3. hi... thanks a lot for replying my message.

i still have the same problem.. for you know, i'm using gcc compiler and if i compile code tht corrected by you, this warning was out.

2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type

then , when i cast it, like this in the caller funtcion,

total = kira((const int [][])nom);

this warning out.

2d1.c:27: cast specifies array type

.... but , if i compile in windows (using MS Visual C++),,, no errror and output was printed correctly... i dont know wat to do.

thanks again

4. Originally posted by zainulmin
hi... thanks a lot for replying my message.

i still have the same problem.. for you know, i'm using gcc compiler and if i compile code tht corrected by you, this warning was out.

2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type

then , when i cast it, like this in the caller funtcion,

total = kira((const int [][])nom);

this warning ou
2d1.c:27: cast specifies array type

.... but , if i compile in windows (using MS Visual C++),,, no errror and output was printed correctly... i dont know wat to do.

thanks again [/B]
To remove error in GCC compiler, you need to remove CONST in the kira()function since you try to modify this array; so change it to:
int kira(int nom[][3])

Also you can use pointer parameter as 2D-array
such as : int kira(int **nom);
Good luck!
DV007

5. > 2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type
This is very curious.

If you have these functions
void foo ( const int b ) { }
void bar ( const int b[] ) { }
void baz ( const int b[][3] ) { }

With these calls
foo( nom[0][0] );
bar( nom[0] );
baz( nom );

Then it's only the last one which has a problem with the const.

What's more curious is that compiling the code with the GNU C++ compiler does NOT produce the same warning (no warnings at all)

Like so
D:\code>gcc -Wall -x c++ sample.c

D:\code>gcc -Wall sample.c
sample.c: In function `main':
sample.c:34: warning: passing arg 1 of `baz' from incompatible pointer type

And using the MSVC++ compiler compiling the same code as either C or C++ results in no warnings either.

::more digging::
Mmm, perhaps its this bug?

> you need to remove CONST in the kira()function since you try to modify this array
This would work, but the array isn't being modified by the kira() function, so the use of const in this context is correct.

> int kira(int **nom);
This is in no way equivalent to int kira ( int nom[][3] );
Doing this would break the code

6. Perhaps it is a problem with the way they're casting it:

total = kira((const int [][])nom);

Just a thought...

Quzah.

7. Originally posted by Salem
> 2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type
This is very curious.

If you have these functions
void foo ( const int b ) { }
void bar ( const int b[] ) { }
void baz ( const int b[][3] ) { }

With these calls
foo( nom[0][0] );
bar( nom[0] );
baz( nom );

Then it's only the last one which has a problem with the const.

What's more curious is that compiling the code with the GNU C++ compiler does NOT produce the same warning (no warnings at all)

Like so
D:\code>gcc -Wall -x c++ sample.c

D:\code>gcc -Wall sample.c
sample.c: In function `main':
sample.c:34: warning: passing arg 1 of `baz' from incompatible pointer type

And using the MSVC++ compiler compiling the same code as either C or C++ results in no warnings either.

::more digging::
Mmm, perhaps its this bug?

> you need to remove CONST in the kira()function since you try to modify this array
This would work, but the array isn't being modified by the kira() function, so the use of const in this context is correct.

> int kira(int **nom);
This is in no way equivalent to int kira ( int nom[][3] );
Doing this would break the code
I just wonder why it cause to break the code when passing
pointer parameter to the function??? with example above:
Code:
```
int kira(int **nom)
{  int i, j;
int total = 0;
for (i = 0; i <3; i++)
for(j=0; j<3; j++)
total =total + nom[i][j];