Dynamic memory

I need to build a two dimensional array with three columns and a variable number of rows. The number of rows is to be determined by the user's input. I've tried defining an array as follows:

int My_Array[2][rows];

where "rows" is a value defined by the user during program execution (am I right in thinking that runtime is when the program is first run and execution is the period during which it is running?)

can anyone point me in the right direction on this?

Many thanks for your time
That would be incorrect. You have to do it like this:

1
2
3
4
5
int *myArray[2]; //This is a one-dimensional array of two pointers to int.
//Now you obtain the number that is variable from the user.
//Then you instantiate the final arrays:
myArray[0] = new int[rows];
myArray[1] = new int[rows];
First of all I would like to note that the left subscript denotes rows and the right subscript denotes column. Moreover if you are speaking about three columns then the definition of the array shall include the number 3. So it could be as

int My_Array[rows][3];

However the C++ Standard requires that values of subscriptirs shall be constant expression. So the above declaration is invalid because rows is not a constant expression.

So you may allocate such arrayy in dynamical memory on the fly.

int ( *My_Array )[3] = new int[rows][3];
First of all I would like to note that the left subscript denotes rows and the right subscript denotes column.

It doesn't have to be like that. The programmer can do as a he wants.
Last edited on
Thanks very much guys hugely helpful :).
Topic archived. No new replies allowed.