Initialize a static array with struct elements

Discussion in 'C++' started by syang8, Jun 20, 2008.

  1. syang8

    syang8 Guest

    If I have a

    struct S
    {
    int a;
    float b;
    };

    how to initialize a static array of S?

    static struct S sarray[] =
    {
    ?????
    };
    syang8, Jun 20, 2008
    #1
    1. Advertising

  2. syang8

    Eric Pruneau Guest

    "syang8" <> a écrit dans le message de news:
    ...
    > If I have a
    >
    > struct S
    > {
    > int a;
    > float b;
    > };
    >
    > how to initialize a static array of S?
    >
    > static struct S sarray[] =
    > {
    > ?????
    > };


    Example


    struct S
    {
    S() : a(0){} // ctor 1
    explicit S(int i) : a(i){} // ctor 2
    int a;
    };

    int main()
    {
    static S s1[2] = { S(1), S(2) }; // uses ctor 2
    static S s2[2]; // uses ctor 1

    return 0;
    }

    ---------------

    Eric Pruneau
    Eric Pruneau, Jun 20, 2008
    #2
    1. Advertising

  3. syang8

    red floyd Guest

    syang8 wrote:
    > If I have a
    >
    > struct S
    > {
    > int a;
    > float b;
    > };
    >
    > how to initialize a static array of S?
    >
    > static struct S sarray[] =
    > {
    > ?????
    > };


    What book are you reading that doesn't explain POD struct
    initialization? Note that the "struct" in the definition
    of sarray[] is a C-ism, not needed in C++.

    static S sarray[] =
    {
    { 1, 2.3 },
    { 4, 5.6 },
    { 7, 8.9 }
    };
    red floyd, Jun 20, 2008
    #3
  4. syang8

    syang8 Guest

    This answers my question. Thank you.

    You are right. It is a pure C quesion. I think I posted on the wrong
    group.

    On Jun 20, 3:53 pm, red floyd <> wrote:
    > syang8 wrote:
    > > If I have a

    >
    > > struct S
    > > {
    > >   int a;
    > >   float b;
    > > };

    >
    > > how to initialize a static array of S?

    >
    > > static struct S sarray[] =
    > > {
    > >   ?????
    > > };

    >
    > What book are you reading that doesn't explain POD struct
    > initialization?  Note that the "struct" in the definition
    > of sarray[] is a C-ism, not needed in C++.
    >
    > static S sarray[] =
    > {
    >     { 1, 2.3 },
    >     { 4, 5.6 },
    >     { 7, 8.9 }
    >
    >
    >
    > };- Hide quoted text -
    >
    > - Show quoted text -
    syang8, Jun 20, 2008
    #4
  5. syang8

    James Kanze Guest

    On Jun 20, 10:40 pm, syang8 <> wrote:
    > You are right. It is a pure C quesion. I think I posted on the
    > wrong group.


    Not really. In C++, such object types are called POD structs,
    and they are very useful because they allow static
    initialization, and thus avoid order of initialization problems.
    Red Floyd's comment about C-ism only concerned the extra
    "struct" in the data declaration.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 21, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,354
    Chris Fogelklou
    Apr 20, 2004
  2. Mark A. Odell

    Q: volatile struct elements or entire struct?

    Mark A. Odell, Sep 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    680
    Mark A. Odell
    Sep 23, 2004
  3. Replies:
    10
    Views:
    668
  4. Ehud Shapira
    Replies:
    20
    Views:
    1,101
    Ehud Shapira
    Jun 30, 2007
  5. Tuan  Bui
    Replies:
    14
    Views:
    462
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page