943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1830
  • C++ RSS
May 30th, 2008
0

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

Expand Post »
Hello
I have older c functions which I want invoke in new cpp program:
cpp Syntax (Toggle Plain Text)
  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
Similar Threads
yap
Reputation Points: 10
Solved Threads: 1
Newbie Poster
yap is offline Offline
14 posts
since May 2008
May 30th, 2008
0

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

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
May 31st, 2008
0

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

How about casting
int* a = (int*)x; ?
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 31st, 2008
0

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

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jun 1st, 2008
0

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

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
yap
Reputation Points: 10
Solved Threads: 1
Newbie Poster
yap is offline Offline
14 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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:
Previous Thread in C++ Forum Timeline: What I the wrong
Next Thread in C++ Forum Timeline: Expected primary-expression issue





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


Follow us on Twitter


© 2011 DaniWeb® LLC