View Full Version : 2d int array and pointers.
Wade Berkn
06-07-2007, 03:56 AM
i have an object with a 2d int array declared by: int someArray[10][10];
I also have a function that has (int** array) as its sole parameter, my question is how can i make an int** pointer point to that 2d int array member in the object, so that I can pass it to my function?
I've tried this:
MyObject anObject; //declare object
MyObject* object1; //declare pointer
object1=&anObject; //assign pointer
int** pArray;
pArray=object->someArray;
but this gives an error"cannot convert from int* to int** for argument."
Thanks for the help!
Sol_HSA
06-07-2007, 04:19 AM
You'll most likely save yourself a lot of grief by discarding the 2d array and using a normal one-dimensional one instead.
Wade Berkn
06-07-2007, 05:09 AM
You'll most likely save yourself a lot of grief by discarding the 2d array and using a normal one-dimensional one instead.
Um well Im using it represent a game board with 0's and 1's, I dont know how else to do this, is there a better way?
poita
06-07-2007, 05:19 AM
Well, if that function wants to receive a 2d array as input then it's sole parameter shouldn't be int**.
You would have better luck using a function such as:
void myFunction(int pArray[][10]);
/* Note that C++ doesn't care about array bounds so you don't need
to specify the number of rows, however the number of columns is required
to find the address of the array subscripts */
You can call that by using something like this:
int someArray[10][10];
myFunction(someArray);
Note that if myFunction changes pArray then it will also change someArray because they both refer to the same memory.
Wernaeh
06-07-2007, 07:15 AM
Um well Im using it represent a game board with 0's and 1's, I dont know how else to do this, is there a better way?
There are many ways to do multidimensional arrays, and all have their advantages / disadvantages.
Generally, it is possible to use a onedimensional array (with normal pointers) to represent a twodimensional one.
Consider this 3x4 two-dimensional array:
0 1 2 3
4 5 6 7
8 9 10 11
As you can see, I conveniently numbered the entries of the array so that
each entry has a 0-based index.
Now you can map this 3x4 array to a one-dimensional array:
0 1 2 3 4 5 6 7 8 9 10 11
And store it in memory like this.
Obviously, this new, linear array has 3 * 4 elements, exactly as many as the old array.
The question now is how to access such a linearized array.
Essentially, you want to know, given the x and y coordinates from the previous array, how to get to a single coordinate of the linear array.
(i.e. you want to find, from x, y, the index you need to access the linear
array at to get your desired value).
Since you stored the array as consecutive rows in the linear array, for each complete row into y direction, you need to move by 4 indices within the linear array. Then, you need to move by the remaining row coordinate x.
Say you want to find the index for y = 1, x = 1. Looking into the first diagram,
it is easy to see that the desired index would be 5. But how do we calculate that ?
Using the scheme described above, we have one completed row in y direction, and at the row y = 1, we are at x = 1, so we have one entry to jump over for the current row. The desired linear index then is 4 * 1 + 1 = 5, which we also would have expected.
In general:
Let x, y be indices in a height-width-sized twodimensional array. Then, any associated onedimensional array has a length of height * width, and the associated index of x, y in the linear array is y * width + x.
This is the standard version of doing two-dimensional arrays when height and width need not change independently.
Alternative versions for two-dimensional arrays include manual memory allocation (see malloc() and free() in your compiler's manual), as well as using std::vector (see the STL documentation, C++ only) in various forms. Note each of these may actually be realized multi-dimensional or as a linearized array.
Hope this helps,
Cheers,
- Wernaeh
.oisyn
06-07-2007, 07:51 AM
Alternative versions for two-dimensional arrays include manual memory allocation (see malloc() and free() in your compiler's manual)
If you're using C++, use new[]/delete[] rather than malloc/free. Much safer when dealing with user-defined types.
Another alternative is using boost::multi_array for arbitrary multidimensional arrays.
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.