Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 6:08am
Comments
mbue at 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: bad
good
Please Sign up or sign in to vote.

Solution 2

Please see this[^] and this[^] question from comp.lang.c[^].
The answers to these two questions contain all you need. Smile | :)
 
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. Smile | :)
  Permalink  
v2
Comments
SAKryukov at 11-Mar-11 21:43pm
   
Agree with you, my 5.
--SA
Nuri Ismail at 12-Mar-11 11:23am
   
Thank you very much! :)
Espen Harlinn at 13-Mar-11 5:22am
   
Nice reply, my 5 :)
Nuri Ismail at 14-Mar-11 6:18am
   
Thanks! :)
Rate this: bad
good
Please Sign up or sign in to vote.

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);
  Permalink  
Comments
Espen Harlinn at 13-Mar-11 5:26am
   
my 5
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
SAKryukov at 13-Mar-11 15:20pm
   
My 5, in connection to the new Question by OP.
--SA
Espen Harlinn at 13-Mar-11 15:30pm
   
Thank you, SAKryukov!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use fun(x) instead of fun(&x).
You also have one ( too many, the compiler should complain about that though Smile | :)
  Permalink  
v2
Comments
Khalid Sabtan at 11-Mar-11 11:40am
   
fun(x) did not works , i have tried it before i post the question
Thaddeus Jones at 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 at 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.
Thaddeus Jones at 11-Mar-11 12:13pm
   
You are correct, thanks for the link.
Khalid Sabtan at 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)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 13 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100