passing a matrix

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: drdolittle is an unknown quantity at this point 
Solved Threads: 0
drdolittle drdolittle is offline Offline
Newbie Poster

passing a matrix

 
0
  #1
Nov 6th, 2008
Can somebody explain whats wrong in the following code ? I am trying to
pass a matrix and the called function is a double pointer.
  1. #include <iostream>
  2.  
  3. void func(int **);
  4.  
  5. int main(int argc,char *argv[])
  6. {
  7. int A[2][2]={1,2,3,4};
  8. func(A);
  9. }
  10. void func(int **A)
  11. {
  12. }


Best,
Pradeep
Last edited by drdolittle; Nov 6th, 2008 at 8:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: passing a matrix

 
0
  #2
Nov 6th, 2008
My first thought is because int** is not the same as int[][], though the syntactic use of each is quite similar.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,677
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: passing a matrix

 
0
  #3
Nov 6th, 2008
Originally Posted by Lerner View Post
My first thought is because int** is not the same as int[][], though the syntactic use of each is quite similar.
You're quite right. The int** is a pointer that would be allocated an array of pointers, each of which point to an array of ints. int[][] is a contiguous block of memory, which can only be accessed correctly when the column dimension of the argument array and the column size in the function's formal parameter are the same.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: passing a matrix

 
0
  #4
Nov 7th, 2008
  1. //--------------------------------------
  2.  
  3. int A[2][2]={1,2,3,4}; //--overflow
  4. func(A);
  5.  
  6. //--------------------------------------
  7.  
  8. int A[1][4];
  9. A[0][0] = 1;
  10. A[0][1] = 2;
  11. A[0][3] = 3;
  12. A[0][4] = 4;
  13.  
  14. //--------------------------------------
Last edited by cikara21; Nov 7th, 2008 at 3:11 am.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: passing a matrix

 
0
  #5
Nov 7th, 2008
Originally Posted by cikara21 View Post
  1. //--------------------------------------
  2.  
  3. int A[2][2]={1,2,3,4}; //--overflow
  4. func(A);
  5.  
  6. //--------------------------------------
  7.  
  8. int A[1][4];
  9. A[0][0] = 1;
  10. A[0][1] = 2;
  11. A[0][3] = 3;
  12. A[0][4] = 4;
  13.  
  14. //--------------------------------------
What? Where is overflow?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: drdolittle is an unknown quantity at this point 
Solved Threads: 0
drdolittle drdolittle is offline Offline
Newbie Poster

Re: passing a matrix

 
0
  #6
Nov 7th, 2008
okay, but nobody has suggested how to correct the error. I think its the function declaration which has an error.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: passing a matrix

 
0
  #7
Nov 7th, 2008
Try
  1. void func(int (*)[2]); // declaration
  2.  
  3. void func(int (*x)[2])
  4. {
  5. // whatever
  6. }
In this exmaple, func accepts an argument of type "pointer to array of two ints".

The initialisation of A in your example should be something like;
  1. int A[2][2]={{1,2},{3,4}};
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,677
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: passing a matrix

 
0
  #8
Nov 7th, 2008
Originally Posted by drdolittle View Post
okay, but nobody has suggested how to correct the error. I think its the function declaration which has an error.
The solution is to decide how you want to allocate the matrix, and use function parameter that is formatted the same.
  1. //either
  2. void func(int [][2]);
  3.  
  4. int main(int argc,char *argv[])
  5. {
  6. int A[2][2]={1,2,3,4};
  7.  
  8. func(A);
  9. }
  10. void func(int A[][2])
  11. {
  12. }
  13.  
  14. // --- OR ---
  15.  
  16. void func(int **);
  17.  
  18. int main(int argc,char *argv[])
  19. {
  20. int **A;
  21. A = new int *[2];
  22. A[0] = new int[2];
  23. A[1] = new int[2];
  24. //now go store data in the matrix
  25.  
  26. func(A);
  27. }
  28. void func(int **A)
  29. {
  30. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: passing a matrix

 
0
  #9
Nov 7th, 2008
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: passing a matrix

 
0
  #10
Nov 7th, 2008
Use reinterpret_cast or c style cast to casting..
But the matrix will become 1 dimension matrix..

  1. #include <iostream>
  2.  
  3. void func(int **);
  4.  
  5. int main(int argc,char *argv[])
  6. {
  7. int A[2][2]={1,2,3,4};
  8. //--reinterpret_cast
  9. func(reinterpret_cast<int**>(A));
  10. //--c style cast
  11. func((int**)A); //--func(A);
  12. }
  13. void func(int **A)
  14. {
  15. }


Best,
Pradeep[/QUOTE]
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC