how casting int x[3][2] to int** y ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 14
Reputation: yap is an unknown quantity at this point 
Solved Threads: 1
yap yap is offline Offline
Newbie Poster

how casting int x[3][2] to int** y ?

 
0
  #1
May 30th, 2008
Hello
I have older c functions which I want invoke in new cpp program:
  1. // Example of old c function
  2. void c_function(int** x, int r, int c)
  3. { int i, j;
  4. for (i = 0; i < r; i++)
  5. {
  6. for (j=0; j<c;i++)
  7. printf("%i ", *(*(x+i)+j));
  8. printf("\n");
  9. }
  10. }
  11.  
  12. // my cpp program, where I would like to invoke c_function
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. int r=3, c=2,
  17. x[3][2] =
  18. {
  19. {00, 01},
  20. {10, 11},
  21. {20, 21}
  22. };
  23. c_function(x, r, c);
  24. return 0;
  25. }

This does not work. Error in line c_function(x, r, c):
Conversion of first parameter from 'int [3][2]' to 'int **' impossible. (Sorry, no good translation of foreign language message.)

Question: Is it possible to cast 'int [3][2]' to 'int **' ?

I don't like to use ** pointer in cpp program when [][] for two dimensional arrays is possible.

Thanks a lot for your advice.
yap
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: how casting int x[3][2] to int** y ?

 
0
  #2
May 30th, 2008
The problem is that an array declared as:
int a[ 42 ][ 17 ];
is not the same as
int *a[ 42 ];

The first is a 2D array of 714 (42 x 17) ints.
The second is a 1D array of 42 pointers to int.

The c_function() is looking for something like the second, but you are trying to give it something like the first.

[edit] It is a vagary of the C language that lets you treat pointers like arrays, so however you define it, you can reference a specific int as:
a[ 10 ][ 4 ]

For the first type, it is just a simple multiplication and one dereference to index the element:
*(a + (10 * 42) + 4)

For the second type, it is two dereferences:
*(*(a + 10) + 4)

Hope this helps.
Last edited by Duoas; May 30th, 2008 at 8:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: how casting int x[3][2] to int** y ?

 
0
  #3
May 31st, 2008
How about casting
int* a = (int*)x; ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: how casting int x[3][2] to int** y ?

 
0
  #4
May 31st, 2008
Casting won't change the type of thing found found in the array. It will only make the program look at it as if it were a different type -- which in this case is not the correct type.

You can either fix the original function to take the correct type, or you can fix the thing you are passing to it to be the correct type.

BTW, I presume it is a typo at the end of line 6 to i++ instead of j++

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 14
Reputation: yap is an unknown quantity at this point 
Solved Threads: 1
yap yap is offline Offline
Newbie Poster

Re: how casting int x[3][2] to int** y ?

 
0
  #5
Jun 1st, 2008
yes, its a typo, should have been j.
Now I am convinced that the problem can't be solved with simple cast. What I can do is 1) Changing old c-functions to modern cpp syntax, where the problem with [][const x] still exists. This is also an expensive way. 2) I introduce ** syntax for 2d arrays in cpp program what would allow to use old c functions. Than I need to allocate a vector (first *) and the pointers to further allocated vectors (second *) are stored in first vector. I think this will be the best solution.
Thanks to everybody who has helped me.
yap
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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