From: martin DOT knotek AT berit DOT cz (Martin Knotek) Subject: RE: B20: typedef bug? 20 Jan 1999 18:33:49 -0800 Message-ID: <01BE446A.13157CB0.martin.knotek.cygnus.gnu-win32@berit.cz> Reply-To: "martin DOT knotek AT berit DOT cz" Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: 8bit To: "'gnu-win32 AT cygnus DOT com'" On 2. dubna 1995 12:41, exe [SMTP:Geed AT kong DOT net] wrote: ?????????????? > typedef char bmp_t[480][640]; > > void main() { > bmp_t*bmp=new bmp_t; > } > > results in... > > test.cc: In function `int main(...)': > test.cc:4: initialization to `char (*)[480][640]' from `char (*)[640]' > > This occures in 3 different compilers (including non gnu-cpp), so mabey typedef was not intended to be used this way. > It does appear to be a bogus error though. Hmm, from searching "new" in MSDN doc: .... When new is used to allocate a multidimensional array of objects, it yields a pointer to the first element of the array, and the resultant type preserves the size of all but the leftmost array dimension. For example: new float[10][25][10] yields type float (*)[25][10]. Therefore, the following code will not work because it attempts to assign a pointer to an array of float with the dimensions [25][10] to a pointer to type float: float *fp; fp = new float[10][25][10]; The correct expression is: float (*cp)[25][10]; cp = new float[10][25][10]; The definition of cp allocates a pointer to an array of type float with dimensions [25][10] — it does not allocate an array of pointers. All but the leftmost array dimensions must be constant expressions that evaluate to positive values; the leftmost array dimension can be any expression that evaluates to a positive value. When allocating an array using the new operator, the first dimension can be zero — the new operator returns a unique pointer. .... The above is clear, but how to define an array of pointers to functions? This seems to work: void *fnctarr[3] = {(void*)&f1, (void*)&f2, NULL}; void (*fnctp)() = fnctarr[0]; but the ugly typecasting:(( > the only good workaround I found is to create a macro that typecasts another type > > #define new2(a) ((a*)(new char[sizeof(a)])) > > bmp_t*bmp=new2 (bmp_t); No comments on this - maybe someone more capable? Martin Knotek - For help on using this list (especially unsubscribing), send a message to "gnu-win32-request AT cygnus DOT com" with one line of text: "help".