Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

invalid array

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is this not valid?
int [] a, []b;
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
why is this not valid?
int [] a, []b;


Hi Cathy,
BTW,

is perfectly legal! :roll:
This is what I think:
The first set of square brackets is treated by the compiler as denoting the reference type and therefore belongs with int. Only one declaration type is allowed in such a comma delimited declaration, so the second [] seems to be dangling and is treated as part of the variable name, which is not allowed for the start.
I've got hard times figuring out why my example gets compiled as opposed to yours.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the easiest way toi remember is that
e.g int []A,[]A1; // Gives compiler error
b'cos, the "[]" can be only after the primitive type "int" or after the variable A or A1
so
int []A , []A1 is invalid
int []A, A1[] is valid.
Regards
V
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic