I have seen that I can use a variable as the specification for the dimension of a one-dimensional array to be allocated by the "new" keyword. The book I'm reading explains that this can be extended to two or more dimensions but with a restriction, only the left-most dimension can be specified by a variable. All other dimensions must be constants or constant expressions. So I could write something like this:

int max = 0;                        // Number set by user at execution
long* pBigArray = 0;        // Pointer to big array
pBigArray = new long[max] [100] [100];          // Multi-dimensional array

But I can't write this:

pBigArray = new long[max] [max] [max];

Can someone tell me why I can't write the above?
Is there a way to get around this? It seems like there should be a way to dynamically allow multi-dimensional arrays to be set. Like maybe somehow setting three one dimensional arrays dynamically and then use them to form a multi-dimensional array. If that is the case I still don't understand why you can't just set a multi-dimensional array to begin with.
The error I get from my compiler when I try to do the above is:
"error C2440: '=' : cannot convert from 'long (*)[1][1]' to 'long *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

I can't give you a complete answer, but the only way I know how to implement a multi dimensional array is by using multiple pointers, i.e. a 2d array would be implemented by using a double pointer. Search for double pointers in the net and you will probably see a better explanation then one I could give.

As to why it is so, maybe that has to do with the fact that c++ leaves you in charge of everything. It is not like java, whose tendencies to manage stuff for you (garbage collector, lack of expĺicit use of pointers) could enhance your productivity with a tradeoff in performance.

That's my two cents, hope it helps.

GGAB

One more way is to use a single dimension array. You can as easily take all the inputs from the users and then declare a single dimension array dynamically, size would the product of the the dimensions. and then while accessing the elements you can use pointer arithmetic.
something like:

//taking inputs as rows n cols
                int* arr = new int[rows * cols];
	int k = 0;
	for (int i=0;i<rows;i++)
	{
		for (int j=0;j<cols;j++)
		{
			arr[i*cols+j] = k++;
		}
	}
                // print the values row wise
	for (int i=0;i<rows;i++)
	{

		for (int j=0;j<cols;j++)
		{
			cout << arr[i*cols+j];
		}
		cout << "change of row" << endl;
	}

You can easily extend this for 3D arrays now.

> So I could write something like this:
...
no, you can't. you need to write something like:

std::size_t max = 0; // Number set by user at execution
// ...
long (*pBigArray)[100][100] = new long [max][100][100];

> Can someone tell me why I can't write the above?
for performing pointer arithmetic (including array subscrpt) correctly, the compiler needs to know (at compile time) the size of the element that is pointed to.

> Is there a way to get around this?
the simplest would be to use std::vector

Thx guys, answered all my questions.

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.