943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1676
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 6th, 2008
0

passing a matrix

Expand Post »
Can somebody explain whats wrong in the following code ? I am trying to
pass a matrix and the called function is a double pointer.
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drdolittle is offline Offline
2 posts
since Nov 2008
Nov 6th, 2008
0

Re: passing a matrix

My first thought is because int** is not the same as int[][], though the syntactic use of each is quite similar.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 6th, 2008
0

Re: passing a matrix

Click to Expand / Collapse  Quote originally posted by Lerner ...
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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 7th, 2008
0

Re: passing a matrix

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 7th, 2008
0

Re: passing a matrix

Click to Expand / Collapse  Quote originally posted by cikara21 ...
c++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 7th, 2008
0

Re: passing a matrix

okay, but nobody has suggested how to correct the error. I think its the function declaration which has an error.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drdolittle is offline Offline
2 posts
since Nov 2008
Nov 7th, 2008
0

Re: passing a matrix

Try
C++ Syntax (Toggle Plain Text)
  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;
C++ Syntax (Toggle Plain Text)
  1. int A[2][2]={{1,2},{3,4}};
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Nov 7th, 2008
0

Re: passing a matrix

Click to Expand / Collapse  Quote originally posted by drdolittle ...
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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 7th, 2008
0

Re: passing a matrix

kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Nov 7th, 2008
0

Re: passing a matrix

Use reinterpret_cast or c style cast to casting..
But the matrix will become 1 dimension matrix..

c++ Syntax (Toggle Plain Text)
  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]
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC