Jump to page: 1 2
Thread overview
How do I initialize arrays in D????
Feb 23, 2005
Ron
Feb 23, 2005
Derek Parnell
Feb 23, 2005
John Reimer
Feb 23, 2005
John Reimer
Feb 23, 2005
Manfred Nowak
Feb 23, 2005
John Reimer
Feb 23, 2005
David Medlock
Feb 23, 2005
John Reimer
Feb 23, 2005
Georg Wrede
Feb 24, 2005
Ron
Feb 24, 2005
brad
Feb 24, 2005
Derek
Feb 24, 2005
Ron
Feb 24, 2005
Derek Parnell
Feb 24, 2005
Kris
Feb 24, 2005
Chris Sauls
Feb 23, 2005
Kris
Feb 25, 2005
Dave
February 23, 2005
The C way of:

void[16] *routines = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};

does NOT work. I get the message:
"Error: a struct is not a valid initializer for a void[16]*"

Same thing with a char array (same way, just w/ string data), but the error
message is:
"Error: a struct is not a valid initializer for a char[32]*"

How do you init these arrays?

--Ron


February 23, 2005
On Wed, 23 Feb 2005 07:11:23 +0000 (UTC), Ron wrote:

> The C way of:
> 
> void[16] *routines = {
> 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0
> };
> 
> does NOT work. I get the message:
> "Error: a struct is not a valid initializer for a void[16]*"
> 
> Same thing with a char array (same way, just w/ string data), but the error
> message is:
> "Error: a struct is not a valid initializer for a char[32]*"
> 
> How do you init these arrays?
> 
> --Ron

There is currently no way to initialize arrays to a literal (static) value at compile time. All array initialization must be done at run time. BTW, most things are automatically initialized to zero/null on declaration.

To initialize an array *before* main() gets control, use a module
constructor...

  int[16] val;
  static this()
  {
     // This is run once, just prior to main().
     val[0] = 1;
     val[1] = 3;
     val[2] = 6;
     val[3] = 8;
     val[4] = 2;
     val[5] = 11;
     val[6] = 51;
     val[7] = 8;
     val[8] = 42;
     val[9] = 100;
     val[10] = 90;
     val[11] = 33;
     val[12] = 45;
     val[13] = -32;
     val[14] = 0;
     val[15] = 12;

  }

-- 
Derek
Melbourne, Australia
23/02/2005 6:13:07 PM
February 23, 2005
Ron wrote:
> The C way of:
> 
> void[16] *routines = {
> 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0
> };
> 
> does NOT work. I get the message:
> "Error: a struct is not a valid initializer for a void[16]*"
> 
> Same thing with a char array (same way, just w/ string data), but the error
> message is:
> "Error: a struct is not a valid initializer for a char[32]*"
> 
> How do you init these arrays?
> 
> --Ron
> 
> 

I'm not sure what you intended here.

The above is a single pointer to an array of 16 void types.  So you won't succeed in trying to assign the array values to a single pointer.

So doing this is valid:

# void[16] *routines = null;

But if you wanted to assign 16 values to an array of 16 void pointers, try this:

# void*[16] routines = null;

The above is a nifty shorthand for assigning "null" to all 16 variables in the array.

You could do it the long way like this:

# // Global variable
#
# void*[16] routines = [
#  null, null, null, null, null, null, null, null,
#  null, null, null, null, null, null, null, null
# ];

The above is a static initializer, so it only works for global variables or static variables (variables lasting the lifetime of the program: the local equivalent to a global variable).  Notice that brackets are used instead of braces.

If you wanted to do the above inside a function, you would do:

# static void*[16] routines = [
#  null, null, null, null, null, null, null, null,
#  null, null, null, null, null, null, null, null
# ];

I hope that helps you.

- John R.
February 23, 2005
Ron wrote:
> The C way of:
> 
> void[16] *routines = {
> 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0
> };
> 
> does NOT work. I get the message:
> "Error: a struct is not a valid initializer for a void[16]*"

D uses [ ] instead of { }, for initializing arrays.
See http://www.digitalmars.com/d/arrays.html

However, it only allows it for *static* arrays.
You can also set things at runtime, if you like.

--anders
February 23, 2005
John Reimer wrote:
> Ron wrote:
> 
>> The C way of:
>>
>> void[16] *routines = {
>> 0, 0, 0, 0, 0, 0, 0, 0,
>> 0, 0, 0, 0, 0, 0, 0, 0
>> };
>>
>> does NOT work. I get the message:
>> "Error: a struct is not a valid initializer for a void[16]*"
>>
>> Same thing with a char array (same way, just w/ string data), but the error
>> message is:
>> "Error: a struct is not a valid initializer for a char[32]*"
>>
>> How do you init these arrays?
>>
>> --Ron
>>
>>
> 
> I'm not sure what you intended here.
> 
> The above is a single pointer to an array of 16 void types.  So you won't succeed in trying to assign the array values to a single pointer.
> 
> So doing this is valid:
> 
> # void[16] *routines = null;
> 
> But if you wanted to assign 16 values to an array of 16 void pointers, try this:
> 
> # void*[16] routines = null;
> 
> The above is a nifty shorthand for assigning "null" to all 16 variables in the array.
> 
> You could do it the long way like this:
> 
> # // Global variable
> #
> # void*[16] routines = [
> #  null, null, null, null, null, null, null, null,
> #  null, null, null, null, null, null, null, null
> # ];
> 
> The above is a static initializer, so it only works for global variables or static variables (variables lasting the lifetime of the program: the local equivalent to a global variable).  Notice that brackets are used instead of braces.
> 
> If you wanted to do the above inside a function, you would do:
> 
> # static void*[16] routines = [
> #  null, null, null, null, null, null, null, null,
> #  null, null, null, null, null, null, null, null
> # ];
> 
> I hope that helps you.
> 
> - John R.

I should point out, though, that initializing static/global values to null/0 is not necessary since D does that automatically on program start.
February 23, 2005
In article <cvhaar$31av$1@digitaldaemon.com>, Ron says...
>
>The C way of:
>
>void[16] *routines = {
>0, 0, 0, 0, 0, 0, 0, 0,
>0, 0, 0, 0, 0, 0, 0, 0
>};
>
>does NOT work. I get the message:
>"Error: a struct is not a valid initializer for a void[16]*"
>
>Same thing with a char array (same way, just w/ string data), but the error
>message is:
>"Error: a struct is not a valid initializer for a char[32]*"
>
>How do you init these arrays?

First, D uses square brackets to init arrays:

# int[] x = [1, 2, 3, 4, 5, 6];

When assigning structs, do it like so:

# struct Foo {int a; char[] b;}
# Foo[] foo = [{1, "one"}, {2, "two"}];

And so on. There are a number of things that cannot be initialized in this
manner (yet).

- Kris


February 23, 2005
John Reimer <brk_6502@yahoo.com> wrote:
[...]
> I should point out, though, that initializing static/global values to null/0 is not necessary since D does that automatically on program start.

I think that this is misleading. As Walter pointed out several times
the purpose of the default initializations is to prevent the coder
and tester to have an unreproducible behaviour because of a misssing
initialization. That is, that the default initializing is not meant
to spare the coder the coding of the initializer.
Therefore the coder is to make clear that he wants the initializer to
be identical to the current default initializer value.
On my opinion the specs should make clear that although the default
initializer is defined it is illegal to rely on the value of the
default initializer.

-manfred

February 23, 2005
Manfred Nowak wrote:
> John Reimer <brk_6502@yahoo.com> wrote:
> [...]
> 
>>I should point out, though, that initializing static/global
>>values to null/0 is not necessary since D does that
>>automatically on program start. 
> 
> 
> I think that this is misleading. As Walter pointed out several times the purpose of the default initializations is to prevent the coder and tester to have an unreproducible behaviour because of a misssing initialization. That is, that the default initializing is not meant to spare the coder the coding of the initializer.
> Therefore the coder is to make clear that he wants the initializer to be identical to the current default initializer value.
> On my opinion the specs should make clear that although the default initializer is defined it is illegal to rely on the value of the default initializer.
> 
> -manfred
> 

Good point.  I agree.  It is good practice to do initializations yourself.
February 23, 2005
John Reimer wrote:

> 
> I should point out, though, that initializing static/global values to null/0 is not necessary since D does that automatically on program start.

In doing some stuff with floating point math I was stumped until I noticed the default value for float/double/real is NaN.
(I am not sure why, though I am sure there is a good reason for this)

FYI
February 23, 2005
David Medlock wrote:
> John Reimer wrote:
> 
>>
>> I should point out, though, that initializing static/global values to null/0 is not necessary since D does that automatically on program start.
> 
> 
> In doing some stuff with floating point math I was stumped until I noticed the default value for float/double/real is NaN.
> (I am not sure why, though I am sure there is a good reason for this)
> 
> FYI

True.  I should have clarified that the default values are not always 0.  It depends on the variable type.  So like Manfred said, it's important to realize that the programmer still is responsible for initializing these variables.

- John R.
« First   ‹ Prev
1 2