13,090,930 members (47,750 online)
Rate this:
See more:
i can not send 2 dimension array to a function
```void fun(int **x);
int _tmain(int argc, _TCHAR* argv[])
{
int x[5][6];

for(i=0;i<5;i++)
for(j=0;j<6;j++)
x[i][j]=i+j;

for(i=0;i<5;i++)
for(j=0;j<6;j++)
cout<<"x[i][j]="<<x[i][j]<<endl;

fun(&(x);<-----Error

return 0;
}
void fun(int **x)
{```

.
.
.
Posted 11-Mar-11 5:08am
mbue 11-Mar-11 12:15pm

Use typedef and everything is ok.
``` typedef int I5x6[5][6]; void func(I5x6& z) { unsigned int i,j; for(i=0;i<5;i++) { for(j=0;j<6;j++) { TRACE(__TEXT("%i\r\n"),z[i][j]); } } } void main() { I5x6 ip; unsigned int i,j; for(i=0;i<5;i++) { for(j=0;j<6;j++) { ip[i][j] = i+j; } } func(ip); } ```

Rate this:

## Solution 2

Please see this[^] and this[^] question from comp.lang.c[^].
The answers to these two questions contain all you need. :)

BTW: Your question is tagged as C++ not C. Above links will solve the problem in your C - style code.
If you want to change your code to C++ - style, then you can replace your two dimensional integer array with
`std::vector< std::vector<int> >` (see std::vector[^]) and pass this vector by reference to your function. :)
v2
SAKryukov 11-Mar-11 21:43pm

Agree with you, my 5.
--SA
Nuri Ismail 12-Mar-11 11:23am

Thank you very much! :)
Espen Harlinn 13-Mar-11 5:22am

Nuri Ismail 14-Mar-11 6:18am

Thanks! :)
Rate this:

## Solution 3

Your function should be like this:
```void fun(int x[][6])
{
}```

and you would call it like this:
```int x[5][6];
fun(x);
```
Espen Harlinn 13-Mar-11 5:26am

my 5
Rate this:

## Solution 4

Oddly enough:
```int _tmain(int argc, _TCHAR* argv[])
{
// argv points to an array of c style strings.
// a c style string is a character array where the
// last character is set to '\x0'.

// You had the required declaration for 2 dimensional arrays
// right in front of you all the time.
}```

Regards
Espen Harlinn
SAKryukov 13-Mar-11 15:20pm

My 5, in connection to the new Question by OP.
--SA
Espen Harlinn 13-Mar-11 15:30pm

Thank you, SAKryukov!
Rate this:

## Solution 1

You can use `fun(x)` instead of `fun(&x)`.
You also have one `(` too many, the compiler should complain about that though :)
v2
Khalid Sabtan 11-Mar-11 11:40am

fun(x) did not works , i have tried it before i post the question

What is your exact code? I see more errors in this fragment but I ignored them because I thought you did them intentionally to keep things short. For example this program won't work since you use the variables i and j without declaring them.
Nuri Ismail 11-Mar-11 11:55am

Thaddeus, I think that the most serious problem in the code is using pointer-to-pointer to pass two dimensional arrays to function. Please have a look at the links from my answer, especially the first one.

NOTE: This downvote was not from me. I'm actually against downvotes I'd prefer there are only upvotes.

You are correct, thanks for the link.
Khalid Sabtan 11-Mar-11 12:04pm

i made the code very short,of course i and j is not declared, you could also say where is the include.
but this is good notice next time i will send the whole program

Top Experts
Last 24hrsThis month
 OriginalGriff 180 RickZeeland 85 Graeme_Grant 70 ProgramFOX 70 Wonde Tadesse 60
 OriginalGriff 3,557 Graeme_Grant 1,944 ProgramFOX 1,777 Jochen Arndt 1,645 ppolymorphe 1,562