Ptrs to Multi-Arrays

Started by
6 comments, last by cMADsc 22 years, 4 months ago
Greetings, I have started coding the game Connect 4. Although, I keep running into errors when I try to use a ptr to my multi-array. My c++ books has NO documentation on ptrs to matrix. Any explanation on it and iterating through it is appreciated. Thanks in advance! ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own." Edited by - cMADsc on December 26, 2001 9:10:53 PM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
Advertisement
How did you declare/define the array/matrix?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
An array size s of ''type'', let''s call it ''xyz'', is defined like this:

type xyz;<br><br>In memory, that looks like:<br><br>+—+    +——-+<br>|xyz—-&gt;|xyz[0] |<br>+—+    +——-+<br>         |xyz[1] |<br>         +——-+<br>         |xyz[2] |<br>         +——-+<br>         |xyz[3] |<br>         +——-+<br> </pre> <br><br>An array is, in fact, equivalent to a pointer; the variable ''xyz'' is a pointer to the first element in the array. If you put brackets after that, xyz[n] is the nth element of the array. A 2-D array is an array of arrays, which looks something like this:<br><br><pre><br>type xyz[s1][s2];<br><br><br>+—-+  +——-+  +———+———+———+<br>|xyz—&gt;|xyz[0]—&gt;|xyz[0][0]|xyz[0][1]|xyz[0][2]|<br>|    |  |       |  +———+———+———+<br>+—-+  |       |<br>        +——-+  +———+———+———+<br>        |xyz[1]—&gt;|xyz[1][0]|xyz[1][1]|xyz[1][2]|<br>        |       |  +———+———+———+<br>        |       |<br>        +——-+  +———+———+———+<br>        |xyz[2]—&gt;|xyz[2][0]|xyz[2][1]|xyz[2][2]|<br>        |       |  +———+———+———+<br>        |       |<br>        +——-+  +———+———+———+<br>        |xyz[3]—&gt;|xyz[3][0]|xyz[3][1]|xyz[3][2]|<br>        |       |  +———+———+———+<br>        |       |<br>        +——-+<br> </pre> <br><br>To get from xyz to an actual piece of data, we have to follow pointers (<I>dereference</I>) more than once. The first dereferencing, we go from, for example, xyz to xyz[0], the second dereferencing gets us from xyz[0] to xyz[0][1]. The number of pointers to be followed are called <I>levels of indirection</I>. xyz has two levels of indirection. A pointer to X always has one more level of indirection than X does, so a pointer to xyz has three levels of indirection; that is, it is a pointer to a pointer to a pointer. So, you would declare a pointer to a 2-D array named foo with the line:<br><br><pre><br>type ***foo;<br><br>+—-+   +—-+  +——-+  +———+———+———+<br>|foo—-&gt;|xyz—&gt;|xyz[0]—&gt;|xyz[0][0]|xyz[0][1]|xyz[0][2]|<br>|    |   |    |  |       |  +———+———+———+<br>+—-+   +—-+  |       |<br>                 +——-+  +———+———+———+<br>                 |xyz[1]—&gt;|xyz[1][0]|xyz[1][1]|xyz[1][2]|<br>                 |       |  +———+———+———+<br>                 |       |<br>                 +——-+  +———+———+———+<br>                 |xyz[2]—&gt;|xyz[2][0]|xyz[2][1]|xyz[2][2]|<br>                 |       |  +———+———+———+<br>                 |       |<br>                 +——-+  +———+———+———+<br>                 |xyz[3]—&gt;|xyz[3][0]|xyz[3][1]|xyz[3][2]|<br>                 |       |  +———+———+———+<br>                 |       |<br>                 +——-+<br> </pre> <br><br>To get from foo to xyz[0][1], you would first dereference foo once (*foo), then give two subscripts, to make (*foo)[0][1].<br><br>As you can see, having many levels of indirection can get messy. Another common problem is going out of array bounds. If I have an array X with 5 elements, and then I try to refer to X[7], that is an error and will crash. In C, it is customary to pass size information around separately; in C++, you might make container classes. (Which, by the way, is a different and complex topic).<br><br>(I hope those pointer graphs came out OK. If they didn''t, I''ll try to edit it until they do, but barring that, copy it into an editor with a monospace font.)    <br><br>—————<br>A picture is worth 1000 words, 500 longwords, or 250 dwords.    
---------------A picture is worth 1000 words, 500 longwords, or 250 dwords.
I love those pictures.
  // a multi-arrayint board[6][4];// point to the beginningint main(int argc, char** args){    int* pBoard = &board[0][0];    int x;    x = pBoard[0]; // x = board[0][0];    x = pBoard[3]; // x = board[0][3];    x = pBoard[7]; // x = board[1][1];    return 0;}  


I think that should work, but I could be wrong

No, HTML is not an OO language.
i have my matrix declared in main and trying to pass it by refrences to a function....

int Move(char *);
int main()
{
const int row=6, col=7;
char Matrix[row][col];
char *pMatrix= &matrix[0][0];
.
.
.
return 0;
}

Matrix
==========
|   |   |   |   |   |   |   |+---+---+---+---+---+---|---+|   |   |   |   |   |   |   |+---+---+---+---+---+---|---+|   |   |   |   |   |   |   |+---+---+---+---+---+---|---+|   |   |   |   |   |   |   |+---+---+---+---+---+---|---+|   | o |   |   |   |   |   |+---+---+---+---+---+---|---+| x | x |   | o |   |   |   |+---+---+---+---+---+---|---+  1   2   3   4   5   6   7  


Before inserting a peice i need to check each col to see if its value is '\0', if so insert 'x' or 'o'. each 'x' or 'o' will be
places at the bottom. can i iterate through the matrix with a FOR loop like
========================|  |==================================== for (int i=0; i<7; i++)|or| do i have use pointer arithmetic? pMatrix[row];        |  |  *pMatrix++;========================|  |==================================== 
int Move(char *pMatrix)
{

damn mental block!!!



Edited by - cMADsc on December 27, 2001 12:34:11 AM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
yes you can use a for loop...
  for(int i=0; i<rows; i++)for(int j=0; <cols; j++)if(0==matrix[i][j])matrix[i][j]=''x'';  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
To pass the matrix[row][col] to Move(), it should be declared as:

int Move (char matrix[][col]);

Then to call Move() from main() you just do:
Move (matrix);

and matrix can be accessed with the usual [row][col] syntax from within Move()


Ok, now I''m going to try to explain this... (somebody please correct me if I''m wrong)

There are two types of multi-dimensional arrays in C++. When an array is allocated dynamically, it is declared as something like
char **array;
and when an element accessed, the compiler creates code which dereferences twice;

If an array is declared statically, such as:
char array[5][10];
then it is created in a single block of memory and to access an element, only one dereference needs to be done. For example, to access element [2][3] from array[5][10], the compiler does this:
array[2 * 10 + 3];

This is why when passing a statically declared array to a function, you don''t need to pass the number inside the first [], because the compiler doesn''t need this in order to access the array elements.

This topic is closed to new replies.

Advertisement