## digitalmars.D.bugs - struct/array static initialization bug

"Andrew Fedoniouk" <news terrainformatica.com> writes:
```Program below outputs two lines:
2 0
2 2

First line result is a bug.
{ two:2 } should behave similar to int two[2] = 2 , I guess.
Now { two:2 } initializes only first element of the array.

---------------------------------------
import std.stdio;
struct A
{
int one;
int two[2];
}
A a = { one:1, two:2 };

int two[2] = 2;

int main(char[][] args)
{
writef("%d %d\n",a.two[0],a.two[1]);
writef("%d %d\n",two[0],two[1]);
return 0;
}
---------------------------------------
```
Mar 09 2005
"Regan Heath" <regan netwin.co.nz> writes:
```On Wed, 9 Mar 2005 08:45:55 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
Program below outputs two lines:
2 0
2 2

First line result is a bug.
{ two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

Now { two:2 } initializes only first element of the array.

Indeed, so should it be the same as two[] = 2, or is it okay as is given
that we can say two:[2,2], eg.

struct A
{
int one;
int two[2];
}

A a = { one:1, two:[2,3] };

void main()
{
printf("%d,%d,%d",a.one,a.two[0],a.two[1]);
}

---------------------------------------
import std.stdio;
struct A
{
int one;
int two[2];
}
A a = { one:1, two:2 };

int two[2] = 2;

int main(char[][] args)
{
writef("%d %d\n",a.two[0],a.two[1]);
writef("%d %d\n",two[0],two[1]);
return 0;
}
---------------------------------------

Regan
```
Mar 09 2005
"Andrew Fedoniouk" <news terrainformatica.com> writes:
``` { two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

I mean following:
static real rr[4] = 3.1415926;
all four elements of rr will be initialized to Pi value.

but if rr is member of struct this
multiple-element-inititalization-by-single-value does not work.

It does work in fact as compiler is happy seeing this

struct A {  int one; int two[4]; }
A a = { one:1, two:2 };

But it is just unexpected initialization behaviour, I guess.

Andrew.

"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsnd5c7q723k2f5 nrage.netwin.co.nz...
On Wed, 9 Mar 2005 08:45:55 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
Program below outputs two lines:
2 0
2 2

First line result is a bug.
{ two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

Now { two:2 } initializes only first element of the array.

Indeed, so should it be the same as two[] = 2, or is it okay as is given
that we can say two:[2,2], eg.

struct A
{
int one;
int two[2];
}

A a = { one:1, two:[2,3] };

void main()
{
printf("%d,%d,%d",a.one,a.two[0],a.two[1]);
}

---------------------------------------
import std.stdio;
struct A
{
int one;
int two[2];
}
A a = { one:1, two:2 };

int two[2] = 2;

int main(char[][] args)
{
writef("%d %d\n",a.two[0],a.two[1]);
writef("%d %d\n",two[0],two[1]);
return 0;
}
---------------------------------------

Regan

```
Mar 09 2005
"Regan Heath" <regan netwin.co.nz> writes:
```On Wed, 9 Mar 2005 23:40:04 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
{ two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

I mean following:
static real rr[4] = 3.1415926;
all four elements of rr will be initialized to Pi value.

I see what you mean. I had it confused with:

real rr[4];
rr[] = 3.1415926;

which also sets all elements of rr to Pi value.

but if rr is member of struct this
multiple-element-inititalization-by-single-value does not work.

No, instead it sets only the first element.

It does work in fact as compiler is happy seeing this

struct A {  int one; int two[4]; }
A a = { one:1, two:2 };

But it is just unexpected initialization behaviour, I guess.

I actually expected it to only set the first element, but, I'm used to C
which does not have a 'set all elements of an array' feature (unless you
count the memset function).

I think it would be nice to be able to do both. Given that we can write:

A a = { one:1, two:[2,0,0,0] };

or, shorthand:

A a = { one:1, two:[2] };

to set 'just the first element to 2' it would make sense for:

A a = { one:1, two:2 };

to set all elements to '2'.

Regan
```
Mar 10 2005
"Andrew Fedoniouk" <news terrainformatica.com> writes:
``` I think it would be nice to be able to do both. Given that we can write:

Yes. But in any case compiler must be consistent in these two places.

Andrew.

"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsne181pg23k2f5 nrage.netwin.co.nz...
On Wed, 9 Mar 2005 23:40:04 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
{ two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

I mean following:
static real rr[4] = 3.1415926;
all four elements of rr will be initialized to Pi value.

I see what you mean. I had it confused with:

real rr[4];
rr[] = 3.1415926;

which also sets all elements of rr to Pi value.

but if rr is member of struct this
multiple-element-inititalization-by-single-value does not work.

No, instead it sets only the first element.

It does work in fact as compiler is happy seeing this

struct A {  int one; int two[4]; }
A a = { one:1, two:2 };

But it is just unexpected initialization behaviour, I guess.

I actually expected it to only set the first element, but, I'm used to C
which does not have a 'set all elements of an array' feature (unless you
count the memset function).

I think it would be nice to be able to do both. Given that we can write:

A a = { one:1, two:[2,0,0,0] };

or, shorthand:

A a = { one:1, two:[2] };

to set 'just the first element to 2' it would make sense for:

A a = { one:1, two:2 };

to set all elements to '2'.

Regan

```
Mar 10 2005
"Regan Heath" <regan netwin.co.nz> writes:
```On Thu, 10 Mar 2005 12:05:07 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
I think it would be nice to be able to do both. Given that we can write:

Yes. But in any case compiler must be consistent in these two places.

And what do you think is consistent?
Because from where I'm sitting, it *is* consistent already. Consistent
with C/C++ behaviour.

Regan

Andrew.

"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsne181pg23k2f5 nrage.netwin.co.nz...
On Wed, 9 Mar 2005 23:40:04 -0800, Andrew Fedoniouk
<news terrainformatica.com> wrote:
{ two:2 } should behave similar to int two[2] = 2 , I guess.

I think you meant two[] = 2?

I mean following:
static real rr[4] = 3.1415926;
all four elements of rr will be initialized to Pi value.

I see what you mean. I had it confused with:

real rr[4];
rr[] = 3.1415926;

which also sets all elements of rr to Pi value.

but if rr is member of struct this
multiple-element-inititalization-by-single-value does not work.

No, instead it sets only the first element.

It does work in fact as compiler is happy seeing this

struct A {  int one; int two[4]; }
A a = { one:1, two:2 };

But it is just unexpected initialization behaviour, I guess.

I actually expected it to only set the first element, but, I'm used to C
which does not have a 'set all elements of an array' feature (unless you
count the memset function).

I think it would be nice to be able to do both. Given that we can write:

A a = { one:1, two:[2,0,0,0] };

or, shorthand:

A a = { one:1, two:[2] };

to set 'just the first element to 2' it would make sense for:

A a = { one:1, two:2 };

to set all elements to '2'.

Regan

```
Mar 13 2005
=?UTF-8?B?VGhvbWFzIEvDvGhuZQ==?= writes:
```-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Fedoniouk wrote:

|>>{ two:2 } should behave similar to int two[2] = 2 , I guess.
|>
|>I think you meant two[] = 2?
|
|
| I mean following:
| static real rr[4] = 3.1415926;
| all four elements of rr will be initialized to Pi value.
|
| but if rr is member of struct this
| multiple-element-inititalization-by-single-value does not work.
|
| It does work in fact as compiler is happy seeing this
|
| struct A {  int one; int two[4]; }
| A a = { one:1, two:2 };
|
| But it is just unexpected initialization behaviour, I guess.

http://dstress.kuehne.cn/run/struct_initialization_06.d

Thomas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFCNTVI3w+/yD4P9tIRAgwDAJ9UTX0CjGBbSRW3Lfopa13IRUhzZgCgikI/
70S8tXpErNH4L3qSp7b6c+0=
=rzQ4
-----END PGP SIGNATURE-----
```
Mar 13 2005