Topic: Initialisation of arrays of structs


Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Fri, 1 Sep 2000 17:33:38 GMT
Raw View
Hello,
when I have a struct like

struct S
{
  int m1; int m2;
};

and an array with initialisation:

S s[2] = { 1,2,3,4 };

Is this syntax of the initialiser list possible? I have one compiler
where this works as expected. But I thought it should be more like

S s[2] = {  { 1,2},  {3,4}  };

which works, too. Is the first form really correct Standard-C++ ?



------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: llewelly.@@edevnull.dot.com
Date: Fri, 1 Sep 2000 18:58:41 GMT
Raw View
MikeAlpha@NoSpam_csi.com (Martin Aupperle) writes:

> Hello,
> when I have a struct like
>
> struct S
> {
>   int m1; int m2;
> };
>
> and an array with initialisation:
>
> S s[2] = { 1,2,3,4 };
>
> Is this syntax of the initialiser list possible? I have one compiler
> where this works as expected. But I thought it should be more like
>
> S s[2] = {  { 1,2},  {3,4}  };
>
> which works, too. Is the first form really correct Standard-C++ ?

Both are well-formed. You are allowed to elide matching braces (except
  the outermost pair) from an aggregate initializer. Details in
  8.5.1/11 .

Note that if the braced initializer list does not explicitly
  initialize all the elements of the aggregate, removal of the braces
  can sometimes change the meaning of the initializer.

S s[2] = {{1},{3,4}}; //s[0].m2 initialized to 0, because it is not in
                      //  the list.

S s[2] = {1,3,4}; //s[0].m2 initialized to 3, s[1].m2 initialized to 0.

Fortunately, some compilers will warn about partially braced
  initializers.

[snip]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: comeau@panix.com (Greg Comeau)
Date: Sun, 3 Sep 2000 12:25:47 GMT
Raw View
In article <39af4b83.620449@news.nikoma.de>,
Martin Aupperle <MikeAlpha@NoSpam_csi.com> wrote:
>when I have a struct like
>
>struct S
>{
>  int m1; int m2;
>};
>
>and an array with initialisation:
>
>S s[2] = { 1,2,3,4 };
>
>Is this syntax of the initialiser list possible? I have one compiler
>where this works as expected. But I thought it should be more like
>
>S s[2] = {  { 1,2},  {3,4}  };
>
>which works, too. Is the first form really correct Standard-C++ ?

It looks ok to me.  Note that often it can get tricky if
you leave some initializers out, but since you've specified
them all in this case, it's ok (it's correct Standard C too).

- Greg
--
Comeau Computing / Comeau C/C++ ("so close" 4.2.44 betas starting)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]