Archived

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

Array **

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

Recommended Posts

I don´t have new ANSI c++ book so can someone make me clear doing two dimension dynamical arrays. I tried it myself but get strange assertion failures.

Share on other sites
An array is just a pointer to a place in memory. A two dimensional array is just an array of arrays ... or rather a pointer to memory, where other pointers to arrays stand.

So, in order to create a dynamic two dimensional rectangular array, try this code

  int **myArray;int x = 10;int y = 10;myArray = new int*[ y ];for(int i=0; i

You can then access thing like myArray[2][2].

MK42

Edited by - MK42 on July 16, 2000 2:13:42 PM

Share on other sites

For dynamic arrays see above. To declare a static 2-dimensional array:

int a[3][4];

Share on other sites
Thanks!
hey Dan james what is int[1][1]....;
It´s multidimensional array.

Share on other sites
If you have an array:

int array[2][3];

It will be 2 sets of numbers (accesed starting at 0, going to 1), with 3 numbers in them (again, it would be starting at 0... to 2 for accessing).

So, it could also be declared:

  int array[2][3] = {8,9,6},{4,5,2}; `

array[0][0] is 8
array[0][1] is 9
array[0][2] is 6

array[1][0] is 4
array[1][1] is 5
array[1][2] is 2

Does that help?

- Goblin
"In order to understand the full depth of mankind, you must first seperate the word into its component parts: 'mank' and 'ind'."

Share on other sites
As MK42 stated earlier:

To create a two-dimensional array...

int **myArray;
int x = 10;
int y = 10;
myArray = new int*[ y ];
for(int i=0; i{
myArray[ i ] = new int[ x ];
}

This is NOT the same as:

int myArray[10][10];

The former method, in memory, gives you an array of pointers,
each of which points to an array of ten ints. These ints are
NOT guaranteed to be contiguous in memory.

The latter method gives you a 10x10 block of contiguous ints. A
true 10x10 array.

MK42 also stated that "An array is just a pointer to a place in
memory. A two dimensional array is just an array of arrays ...
or rather a pointer to memory, where other pointers to arrays
stand."

This is also incorrect. A two dimensional array of ints is
simply w by h contiguous ints (where w and h are the dimensions of the array), whereas a two-dimensional array of
pointers to ints is in fact an array of pointers to other
arrays.

The only reason foo=myArray[5][5] works in both cases is
because of C''s type system. If you don''t believe me, try
making an array of ints with MK42''s method and then doing this:

foo = ((int*)myArray)[4][5];

and see if you get the right answer...

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 10
• 13
• 19
• 14
• 9
• Forum Statistics

• Total Topics
632942
• Total Posts
3009346
• Who's Online (See full list)

There are no registered users currently online

×