Array/Struct Initialization Confusion

Discussion in 'C++' started by quadraticformula, Jun 16, 2008.

  1. Hey, quick question for anyone willing to listen.

    I've always wondered why I can initialize an array of structs with
    something like this (where "..." represents the 14 separate values for
    the individual DIALOG struct) and it wll compile perfectly:

    DIALOG darray[] = { {...}, {...}, {...}, {...}, {...} };

    but something like this gives a syntax error (specifically: expected
    primary-expression before '{' token):

    DIALOG darray[5];
    dialog = { {...}, {...}, {...}, {...}, {...} };

    If I use a DIALOG darray[5] declaration (like if I wanted to declare
    it globally) I always have to dereference every DIALOG in the array
    (0-4) and then assign each of the 14 values for the DIALOG struct
    individually, which is a major drag. I would much rather use a
    bracketed list like in the first example, only I need "darray" to be
    global.

    Can someone please explain what the difference is and why the latter
    is incorrect? Or, if you need more specific information, just let me
    know.

    Thanks in advance,
    keeg
    quadraticformula, Jun 16, 2008
    #1
    1. Advertising

  2. quadraticformula

    Jerry Coffin Guest

    In article <daafc171-591b-45af-a5d4-9eb4c651870b@
    56g2000hsm.googlegroups.com>, says...
    > Hey, quick question for anyone willing to listen.
    >
    > I've always wondered why I can initialize an array of structs with
    > something like this (where "..." represents the 14 separate values for
    > the individual DIALOG struct) and it wll compile perfectly:
    >
    > DIALOG darray[] = { {...}, {...}, {...}, {...}, {...} };
    >
    > but something like this gives a syntax error (specifically: expected
    > primary-expression before '{' token):
    >
    > DIALOG darray[5];
    > dialog = { {...}, {...}, {...}, {...}, {...} };


    [ ... ]

    > Can someone please explain what the difference is and why the latter
    > is incorrect? Or, if you need more specific information, just let me
    > know.


    The first is initialization; the second is assignment. Initialization of
    arrays is allowed, but assignment of arrays is not allowed.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 16, 2008
    #2
    1. Advertising

  3. So what I would call the bracket notation is only allowed in
    initialization, not in assignment. Thanks for the quick response!
    quadraticformula, Jun 16, 2008
    #3
    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,372
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    2
    Views:
    735
    David Harmon
    Sep 20, 2006
  3. toton
    Replies:
    5
    Views:
    932
    Victor Bazarov
    Sep 28, 2006
  4. aaragon
    Replies:
    2
    Views:
    615
    James Kanze
    Nov 2, 2008
  5. Tuan  Bui
    Replies:
    14
    Views:
    471
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page