Hi, I'm new to c++ but I'm having a go trying to create a jagged array

``````int** DATA = new int*[10];
DATA[0] = new int[100];        //this works
DATA[1] = new int[100][5];     //but this don't

DATA[1][100][1] = 1;
``````

Thanks

Assuming that it is what I think you are doing:

``````int** DATA = new int*[10]; // This has now got 10x0
``````

This initialises a 2D array called `DATA` with 10 columns.

You will need to therefore set the rows when you are iterating through the …

## All 2 Replies

Assuming that it is what I think you are doing:

``````int** DATA = new int*[10]; // This has now got 10x0
``````

This initialises a 2D array called `DATA` with 10 columns.

You will need to therefore set the rows when you are iterating through the array. Each row `(0.....9)` will have a row.

Something like this:

``````for(unsigned i=0; (i < 10); i++)
{
DATA[i] = new int[5];

for(unsigned j=0; (j < 5); j++)
{
DATA[i][j] = i*j;

std::cout << DATA[i][j] << ' ';
}
std::cout << std::endl;
}
``````

Notice how I have a for loop, which is iterating through 10 times `0...9` and `DATA` is initalised as having 10 columns. I can therefore set each of these columns `DATA[i...j] (0-9)` to be of size 5 (in this instance). Then I can set the values, but, also I can output the values. An example: Ideone

I hope that this helps.

DATA[0] = new int[100]; //this works

Yup, as it should.

DATA[1] = new int[100][5]; //but this don't

Of course it doesn't, because you're initializing an `int*` with an incompatible type. `int[100][5]` is not compatible with `int*`. You could fake it by saying `DATA[1] = new int[100 * 5]` and handle the indexing of the two dimensions manually, but in this case I'd favor an array (or vector, ideally) of array class that can handle variant dimensions without affecting your type. A simple example of the concept (pre-C++11):

``````#include <cstddef>
#include <iostream>
#include <vector>

using namespace std;

namespace JRD
{
template<typename T>
class VariantArray
{
vector<T> _data;
vector<size_t> _rank;
public:
VariantArray(size_t size): _data(size)
{
_rank.push_back(size);
}

VariantArray(size_t size1, size_t size2):  _data(size1 * size2)
{
_rank.push_back(size1);
_rank.push_back(size2);
}

size_t Ranks() const { return _rank.size(); }
size_t RankSize(size_t index) const { return _rank.at(index); }

T& Item(size_t index) { return _data.at(index); }
T& Item(size_t index1, size_t index2) { return _data.at(index1 * _rank.at(1) + index2); }
};
}

int main()
{
vector<JRD::VariantArray<int>> data;

data.push_back(JRD::VariantArray<int>(5));
data.push_back(JRD::VariantArray<int>(2, 5));

// Initialize the arrays for a clean test
for (int m = 0; m < data[0].RankSize(0); m++)
{
data[0].Item(m) = m;
}

for (int m = 0, k = 0; m < data[1].RankSize(0); m++)
{
for (int n = 0; n < data[1].RankSize(1); n++)
{
data[1].Item(m, n) = k++;
}
}

// Display the contents of the arrays
for (int i = 0; i < 2; i++)
{
cout << "data[" << i << "]\n";

switch (data[i].Ranks())
{
case 1:
for (int m = 0; m < data[i].RankSize(0); m++)
{
cout << data[i].Item(m) << '\t';
}

cout << '\n';
break;
case 2:
for (int m = 0; m < data[i].RankSize(0); m++)
{
for (int n = 0; n < data[i].RankSize(1); n++)
{
cout << data[i].Item(m, n) << '\t';
}

cout << '\n';
}

cout << '\n';
break;
default:
cout << "Unrecognized array rank\n";
break;
}
}
}
``````
Be a part of the DaniWeb community

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