Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 12, 2009 how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
My code is as following: struct Point { float x, y, z ; }; int main () { Point[3] pts = [ {1.0, 0.0, 0.0} , {0.0, 1.0, 0.0} , {0.0, 0.0, 1.0} ]; return 0 ; } But, the compiler report "Error: array initializers as expressions are not allowed". Then, how do I initialize an array of struct ? |
February 12, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to westcity | westcity wrote: > My code is as following: > > struct Point { > float x, y, z ; > }; > > int main () > { > Point[3] pts = [ > {1.0, 0.0, 0.0} , > {0.0, 1.0, 0.0} , > {0.0, 0.0, 1.0} > ]; > > return 0 ; > } > > But, the compiler report "Error: array initializers as expressions are not allowed". That message's pretty messed up: array initializers look like [1.0, 0.0, 0.0] in D. > Then, how do I initialize an array of struct ? Replacing the middle lines with these works: Point(1.0, 0.0, 0.0) , Point(0.0, 1.0, 0.0) , Point(0.0, 0.0, 1.0) |
February 12, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel дµ½:
> westcity wrote:
> > My code is as following:
> >
> > struct Point {
> > float x, y, z ;
> > };
> >
> > int main ()
> > {
> > Point[3] pts = [
> > {1.0, 0.0, 0.0} ,
> > {0.0, 1.0, 0.0} ,
> > {0.0, 0.0, 1.0}
> > ];
> >
> > return 0 ;
> > }
> >
> > But, the compiler report "Error: array initializers as expressions are not allowed".
>
> That message's pretty messed up: array initializers look like [1.0, 0.0, 0.0] in D.
>
> > Then, how do I initialize an array of struct ?
>
> Replacing the middle lines with these works:
> Point(1.0, 0.0, 0.0) ,
> Point(0.0, 1.0, 0.0) ,
> Point(0.0, 0.0, 1.0)
Thanks.
|
February 12, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to westcity | westcity Wrote:
> But, the compiler report "Error: array initializers as expressions are not allowed". Then, how do I initialize an array of struct ?
Move the definition out of main (note that ; after the struct isn't required):
struct Point {
float x, y, z;
}
Point[3] pts = [{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}];
void main () {}
Bye,
bearophile
|
February 12, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> westcity Wrote:
>> But, the compiler report "Error: array initializers as expressions are not allowed".
>> Then, how do I initialize an array of struct ?
>
> Move the definition out of main (note that ; after the struct isn't required):
>
> struct Point {
> float x, y, z;
> }
>
> Point[3] pts = [{1.0, 0.0, 0.0},
> {0.0, 1.0, 0.0},
> {0.0, 0.0, 1.0}];
>
> void main () {}
The fact that *does* compile just makes the error message all the more crazy...
|
February 12, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | On 12.02.2009 15:16, Frits van Bommel wrote:
> bearophile wrote:
>> westcity Wrote:
>>> But, the compiler report "Error: array initializers as expressions are not allowed".
>>> Then, how do I initialize an array of struct ?
>>
>> Move the definition out of main (note that ; after the struct isn't required):
>>
>> struct Point {
>> float x, y, z;
>> }
>>
>> Point[3] pts = [{1.0, 0.0, 0.0},
>> {0.0, 1.0, 0.0},
>> {0.0, 0.0, 1.0}];
>>
>> void main () {}
>
> The fact that *does* compile just makes the error message all the more
> crazy...
It should compile inside of main if declared static. It's just the error message that's a bit off.
|
February 13, 2009 Re: how to initialize an array of struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile дµ½:
> westcity Wrote:
> > But, the compiler report "Error: array initializers as expressions are not allowed". Then, how do I initialize an array of struct ?
>
> Move the definition out of main (note that ; after the struct isn't required):
>
> struct Point {
> float x, y, z;
> }
>
> Point[3] pts = [{1.0, 0.0, 0.0},
> {0.0, 1.0, 0.0},
> {0.0, 0.0, 1.0}];
>
> void main () {}
>
> Bye,
> bearophile
It is perfect, thanks.
|
Copyright © 1999-2021 by the D Language Foundation