Thread overview
Something wrong in integer array
Apr 21, 2005
Hengky Hartono
Apr 21, 2005
zwang
Apr 21, 2005
Regan Heath
Apr 22, 2005
Hengky Hartono
Apr 22, 2005
zwang
Apr 23, 2005
Hengky Hartono
April 21, 2005
i am a new in D.

i am using dmd version 0.121

there is something wrong with this:
int[] def = [ 1, 2, 3 ];
the compiler always give the error message:
D:\HPORJECT\Dedicated\coba.d(4): variable coba.main.def is not a static and
cannot have static initializer

i have tried several ways in D, such as int[4] def=[1,2,3]; but it give me the same error message.

i tried: int def[4]={1,2,3}; in D, but it give the same error, it works well in C++ not in D. i have read the document, but i can't found how to solve this.

Help me please.

There's always another option in every aspect of life.
April 21, 2005
Hengky Hartono wrote:
> i am a new in D.
> 
> i am using dmd version 0.121
> 
> there is something wrong with this:
> int[] def = [ 1, 2, 3 ];
> the compiler always give the error message:
> D:\HPORJECT\Dedicated\coba.d(4): variable coba.main.def is not a static and
> cannot have static initializer
> 
> i have tried several ways in D, such as int[4] def=[1,2,3]; but it give me the
> same error message.
> 
> i tried: int def[4]={1,2,3}; in D, but it give the same error, it works well in
> C++ not in D. i have read the document, but i can't found how to solve this.
> 
> Help me please.
> 
> There's always another option in every aspect of life.

try this:
static int[] def = [1, 2, 3];
April 21, 2005
On Thu, 21 Apr 2005 23:25:57 +0800, zwang <nehzgnaw@gmail.com> wrote:
> Hengky Hartono wrote:
>> i am a new in D.
>>  i am using dmd version 0.121
>>  there is something wrong with this:
>> int[] def = [ 1, 2, 3 ];
>> the compiler always give the error message:
>> D:\HPORJECT\Dedicated\coba.d(4): variable coba.main.def is not a static and
>> cannot have static initializer
>>  i have tried several ways in D, such as int[4] def=[1,2,3]; but it give me the
>> same error message.
>>  i tried: int def[4]={1,2,3}; in D, but it give the same error, it works well in
>> C++ not in D. i have read the document, but i can't found how to solve this.
>>  Help me please.
>>  There's always another option in every aspect of life.
>
> try this:
> static int[] def = [1, 2, 3];

I believe Walter plans in future to allow:
  int[] def = [1, 2, 3];

it's just not implemented yet. I have no idea why, but then, I'm not a compiler writer.

Regan


April 22, 2005
In article <d48gnk$7u1$1@digitaldaemon.com>, zwang says...
>
>Hengky Hartono wrote:
>> i am a new in D.
>> 
>> i am using dmd version 0.121
>> 
>> there is something wrong with this:
>> int[] def = [ 1, 2, 3 ];
>> the compiler always give the error message:
>> D:\HPORJECT\Dedicated\coba.d(4): variable coba.main.def is not a static and
>> cannot have static initializer
>> 
>> i have tried several ways in D, such as int[4] def=[1,2,3]; but it give me the same error message.
>> 
>> i tried: int def[4]={1,2,3}; in D, but it give the same error, it works well in C++ not in D. i have read the document, but i can't found how to solve this.
>> 
>> Help me please.
>> 
>> There's always another option in every aspect of life.
>
>try this:
>static int[] def = [1, 2, 3];
thanx zwang. i can compile the code now.

But it give me another error, when i want to see every value inside the array
def.
code:
void main()
{
static int[] def = [ 1, 2, 3 ];

for(int i=0;i<(sizeof(def)/sizeof(int));i++)      //loop for the length of array
printf(array[i]);
}
the error:
D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement
D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement
D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement ...up to 10
error like this.





April 22, 2005
Hengky Hartono wrote:
> In article <d48gnk$7u1$1@digitaldaemon.com>, zwang says...
> 
>>Hengky Hartono wrote:
>>
>>>i am a new in D.
>>>
>>>i am using dmd version 0.121
>>>
>>>there is something wrong with this:
>>>int[] def = [ 1, 2, 3 ];
>>>the compiler always give the error message:
>>>D:\HPORJECT\Dedicated\coba.d(4): variable coba.main.def is not a static and
>>>cannot have static initializer
>>>
>>>i have tried several ways in D, such as int[4] def=[1,2,3]; but it give me the
>>>same error message.
>>>
>>>i tried: int def[4]={1,2,3}; in D, but it give the same error, it works well in
>>>C++ not in D. i have read the document, but i can't found how to solve this.
>>>
>>>Help me please.
>>>
>>>There's always another option in every aspect of life.
>>
>>try this:
>>static int[] def = [1, 2, 3];
> 
> thanx zwang. i can compile the code now.
> 
> But it give me another error, when i want to see every value inside the array
> def.
> code:
> void main()
> {
> static int[] def = [ 1, 2, 3 ];
> 
> for(int i=0;i<(sizeof(def)/sizeof(int));i++)      //loop for the length of array
> printf(array[i]);
> }
> the error: D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement
> D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement
> D:\HPORJECT\Dedicated\coba.d(9): found 'EOF' instead of statement ...up to 10
> error like this.
> 

There're several errors in your code:
0. sizeof(x) is not supported by D. Use the .sizeof property instead.
(See http://www.digitalmars.com/d/property.html)
1. (def.sizeof/int.sizeof) will always be 2 (8/4) no matter how many elements
int[] def contains.  Write def.length to get the number of elements.
(See http://www.digitalmars.com/d/arrays.html)
2. "array" is not defined
3. the first argument of printf should be a string. Either write
printf("%d", def[i]);
or
writef(def[i]); //remember to import std.stdio;

April 23, 2005
thank's zwang. I understand now.