-3
int** DATA = new int*[10];
DATA[0] = new int[100];    //works
DATA[1] = new int[100][5]; //dont work 

DATA[1][100] = 1;

hello, i'm trying to create a jagged array, but need 5 columns. thanks

  • 1
    Use std::vector. Further, the number of dimensions in an array doesn't change, it's part of the type, so you have to declare it as such already. – Ulrich Eckhardt Feb 21 '15 at 17:04

1 Answers1

1

Do you really need a jagged array (different number of columns for each row) or just a two dimensional array, with 5 columns for each row. In both cases Ulrich's advice to prefer std::vector over bare arrays is good, and you should consider it.

However if you absolutely require a two dimensional array created in C-style, the way to do it is

int** p = new int*[10];
for( int i=0; i<10; i++ ) {
    p[i] = new int[20];
}

p[7][17] = 177;

This will create a 2D array with 10 rows and 20 columns that can be accessed using 2D array syntax.

Wilf Rosenbaum
  • 518
  • 3
  • 10
  • Hi. I'm reading market data from csv files, and the total number of rows varies in each file. I could use a big 2D array, however that doesn't seem very efficient. – Graham Lawes Feb 21 '15 at 17:33