• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array dimension  RSS feed

 
Badri Narayanan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this code is not legal ?

Does this mean array dimension are read from left to right ?(Associativity for [] operator).I don't find a reason or simply could understand the reason.Can somebody gimme me more example to explain
Thanks
-Badri
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Badri Narayanan:
Why does this code is not legal ?

Does this mean array dimension are read from left to right ?(Associativity for [] operator).I don't find a reason or simply could understand the reason.Can somebody gimme me more example to explain
Thanks
-Badri


Okay, this code:

int [][] testArray = new int[3][4];

Means to allocate an array of size 3, of int arrays. And in each element of this array, allocate an array of size 4, of ints.

This code:

int [][] testArray = new int[3][];

Means to allocate an array of size 3, of int arrays. But don't bother allocating each element of this array. Instead just set it to null.

So, if this code:

int [][] testArray = new int[][3];

was legal. What do you think the compiler should do? Should it allocate an unknown number of arrays of size 3, of ints. And assign it to elements of an array that won't be allocated?

Henry
[ December 14, 2006: Message edited by: Henry Wong ]
 
Badri Narayanan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!