Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can not send 2 dimension array to a function
C#
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
Comments
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);
}

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. :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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    
Nice reply, my 5 :)
Nuri Ismail 14-Mar-11 6:18am    
Thanks! :)
Your function should be like this:
void fun(int x[][6])
{
}


and you would call it like this:
int x[5][6];
fun(x);
 
Share this answer
 
Comments
Espen Harlinn 13-Mar-11 5:26am    
my 5
Oddly enough:
C++
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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!
You can use fun(x) instead of fun(&x).
You also have one ( too many, the compiler should complain about that though :)
 
Share this answer
 
v2
Comments
Khalid Sabtan 11-Mar-11 11:40am    
fun(x) did not works , i have tried it before i post the question
[no name] 11-Mar-11 11:41am    
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.
[no name] 11-Mar-11 12:13pm    
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900