• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Array declaration

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Please any one can explain me in the following code,
why the line 2 give compliler error.....
----------------------------------
int[] a1[],a2[]; // 1
int []a3,[]a4; // 2
int []a5,a6[]; // 3
int[] a7,a8[]; // 4
-----------------------------------
The line2 similar to

int []a3;
int []a4;

Then why it leads to compiler error.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In line 2 any valid identifier is expected here. So compiler throws an error message.


int []a3, []a4; // 2
change this line to
int [a3],a4[]; // It compile without any error message

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai shakti,

The declaration of the array

int []a3,[]a4; // 2

shows compile time error as the array brace has to be put after the variable. the array a3 declaration is correct since it immedietly follows the datatype, whereas a4 comes after coma. the correct declaration would be

int []a3,a4[]; // 2

Hope this solves ur doubt.

Cheers,

-jibiN


have will, have everything...
 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am still not clear..

int a1,a2;

Both a1 and a2 are type int.

Then why not in arrays as,

int []a1,[]a2;
plz help me...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

int [] a1;
the above statement means that a1 is of type int[].

int[] a1 , a2;
The above statement means that a1 and a2 both are of type int[].

so int[] becomes the type and a1 and a2 becomes variables.

if we write int[] a1[];
this means a1 is an array of type int[](which itself is an array) therefore a1 is of type int[][] i.e. a 2-d array.

but if we write int[] a1 , []a2;
here a1 is the name of the variable but []a2 cannot be resolved as the compiler expects a variable name and [] are not allowed in the name of the variable.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
is a2 is two dimensional while a1 is single dimnesional one.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simple

int []a3,[]a4; // is wrong, need a primitive/object type on left of []a4.

can be fixed by...

int []a3,a4[];

or

int []a3;
int []a4;

Pally
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic