Does anyone know an easy way to do array's of arrays? I'm not 100% sure this is the thing I want though because I just need a simple way to make a loop able to change the list it's comparing. I have the loop done, I just can't figure out how to make the array change, so i was thinking there might be a way to just do arrays of arrays.

Thanks everyone,
matt

## All 9 Replies

`int arrayOfArrays` will make an array of 2 integer arrays of length 3 each. A 2x3 if you will. Please give a better description with some kind of context. It is hard to understand what you are trying to do.

`int arrayOfArrays` will make an array of 2 integer arrays of length 3 each. A 2x3 if you will.

Isn't an array of 3 integers of 4 elements each?

For on the fly sparse arrays, you can try something like:

``````#include <iostream>

int main()
{
using namespace std;

const int ROWS = 3;
const int COLS = 4;

double** dArr = new double*[ROWS];
for(int i = 0; i < ROWS; ++i)
{
dArr[i] = new double[COLS];
for(int j = 0; j < COLS; ++j)
{
dArr[i][j] = i * COLS + j;
}
}

for(int i = 0; i < ROWS; ++i)
{
for(int j = 0; j < COLS; ++j)
{
cout << dArr[i][j] << "\t";
}
cout << '\n';
}
cin.get();
}``````

Isn't an array of 3 integers of 4 elements each?

Nope, the dimensions which we specify at the time of declaration/definition are the actual dimensions of the array. Their indexing is 1 off of the dimensions.

Nope, the dimensions which we specify at the time of declaration/definition are the actual dimensions of the array. Their indexing is 1 off of the dimensions.

Let's see if I finally get this correct for once:

``int array;``

array
array
array
array

Four elements in an a single dimention array.

``int array;``

array
array

array
array

array
array

Correct?

Let's see if I finally get this correct for once:

``int array;``

array
array
array
array

Four elements in an a single dimention array.

No. Valid indices would be 0-2, for a total of 3.

``int array;``

array
array

array
array

array
array

Correct?

Yes

When you allocate an array, it basically tells the compiler "I want an array of N objects of type T". The compiler will lay those out in memory like so:

``````int arr;
|  0  |  1  |  2  |  3  |  4  |``````

Then when you index into the array, it takes the address of the first element of the array and adds the index to that address (it moves the index by the appropriate size of the data type, e.g. 1 byte for char, 4 bytes for a 32-bit int, etc..)

No. Valid indices would be 0-2, for a total of 3.

So I always got it wrong (opposite) in my mind.
Thank you very much.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.