Computer Science Canada Passing 2D array by reference |
Author: | HazySmoke)345 [ Thu Sep 14, 2006 7:06 pm ] | ||
Post subject: | Passing 2D array by reference | ||
Well, yes. I know how to pass a 1D array by reference. I can just use a pointer for the first element and I'm still able to refer to its elements by using the [] symbol. Now... how do you pass 2D arrays? I tried to have two stars before the variable names and I tried to use its elements with square brackets, like this.
And the program crashed. |
Author: | wtd [ Thu Sep 14, 2006 11:14 pm ] |
Post subject: | |
Was the array you passed in properly allocated? |
Author: | OneOffDriveByPoster [ Fri Sep 15, 2006 8:05 am ] | ||
Post subject: | Re: Passing 2D array by reference | ||
HazySmoke)345 wrote: Well, yes. I know how to pass a 1D array by reference. I can just use a pointer for the first element and I'm still able to refer to its elements by using the [] symbol.
Now... how do you pass 2D arrays? I tried to have two stars before the variable names and I tried to use its elements with square brackets, like this.
And the program crashed. Use int **var with dynamically allocated 2-D arrays (where it is a pointer-to pointer-to-int). I suspect the 2-D array you are passing is declared like so: int v[X][Y]; (something else). Properly-allocated multidimenional arrays of both types will give unexpected results if you pass them wrong. |
Author: | wtd [ Fri Sep 15, 2006 1:09 pm ] | ||
Post subject: | |||
Problems with multi-dimensional arrays will eventually teach you to love code like the following.
|
Author: | md [ Fri Sep 15, 2006 5:55 pm ] |
Post subject: | |
Did you write that yourself wtd? And if so... mind if I make a copy of the code in case of need? |
Author: | wtd [ Fri Sep 15, 2006 6:04 pm ] |
Post subject: | |
Cornflake wrote: Did you write that yourself wtd?
Yes, I did. |
Author: | HazySmoke)345 [ Sat Sep 16, 2006 11:36 pm ] | ||
Post subject: | |||
Quote: Was the array you passed in properly allocated?
No, it wasn't...
I see... thanks. Too bad I don't have too many bits to gave away I gave them all to one of the guys in the VB boards, but I'll just distribute whatever's left. |