News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ array tips - Allocating a 2D array with new

Started by ben2ong2, October 06, 2006, 04:56:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (John A. Elliott)

1) If you want to dynamically declare a multi-dim array, C++ should allow:

int *ia = new int [10][10];


RESPONSE: You are not allowed to view links. Register or Login (Andrew Koenig)
     AT&T Bell Laboratories, Murray Hill NJ


Not quite.  If T is an array type, the type of `new T' is
`pointer to element of T.'  So, if you allocate an array of
arrays, what you get back should be a pointer to the initial
element of that array, which is itself an array.

You should therefore write

   int (*ia)[10] = new int [10][10];

Note that the first 10 after "new" can be an arbitrary expression
but the second one cannot -- it becomes part of the type of the result.

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login