#### Archived

This topic is now archived and is closed to further replies.

# Ptrs to Multi-Arrays

This topic is 6191 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
How did you declare/define the array/matrix?

##### Share on other sites
An array size s of ''type'', let''s call it ''xyz'', is defined like this:

type xyz[s];In memory, that looks like:+---+    +-------+|xyz---->|xyz[0] |+---+    +-------+         |xyz[1] |         +-------+         |xyz[2] |         +-------+         |xyz[3] |         +-------+

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:

type xyz[s1][s2];+----+  +-------+  +---------+---------+---------+|xyz--->|xyz[0]--->|xyz[0][0]|xyz[0][1]|xyz[0][2]||    |  |       |  +---------+---------+---------++----+  |       |        +-------+  +---------+---------+---------+        |xyz[1]--->|xyz[1][0]|xyz[1][1]|xyz[1][2]|        |       |  +---------+---------+---------+        |       |        +-------+  +---------+---------+---------+        |xyz[2]--->|xyz[2][0]|xyz[2][1]|xyz[2][2]|        |       |  +---------+---------+---------+        |       |        +-------+  +---------+---------+---------+        |xyz[3]--->|xyz[3][0]|xyz[3][1]|xyz[3][2]|        |       |  +---------+---------+---------+        |       |        +-------+

To get from xyz to an actual piece of data, we have to follow pointers (dereference) 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 levels of indirection. 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:

type ***foo;+----+   +----+  +-------+  +---------+---------+---------+|foo---->|xyz--->|xyz[0]--->|xyz[0][0]|xyz[0][1]|xyz[0][2]||    |   |    |  |       |  +---------+---------+---------++----+   +----+  |       |                 +-------+  +---------+---------+---------+                 |xyz[1]--->|xyz[1][0]|xyz[1][1]|xyz[1][2]|                 |       |  +---------+---------+---------+                 |       |                 +-------+  +---------+---------+---------+                 |xyz[2]--->|xyz[2][0]|xyz[2][1]|xyz[2][2]|                 |       |  +---------+---------+---------+                 |       |                 +-------+  +---------+---------+---------+                 |xyz[3]--->|xyz[3][0]|xyz[3][1]|xyz[3][2]|                 |       |  +---------+---------+---------+                 |       |                 +-------+

To get from foo to xyz[0][1], you would first dereference foo once (*foo), then give two subscripts, to make (*foo)[0][1].

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).

(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.)

---------------
A picture is worth 1000 words, 500 longwords, or 250 dwords.

##### Share on other sites
I love those pictures.

##### Share on other sites
  // 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.

##### Share on other sites
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

##### Share on other sites
yes you can use a for loop...
 ` for(int i=0; i

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
24
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633651
• Total Posts
3013128
×