Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array/Struct Initialization Confusion

Reply
Thread Tools

Array/Struct Initialization Confusion

 
 
quadraticformula
Guest
Posts: n/a
 
      06-16-2008
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
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      06-16-2008
In article <daafc171-591b-45af-a5d4-9eb4c651870b@
56g2000hsm.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
quadraticformula
Guest
Posts: n/a
 
      06-16-2008
So what I would call the bracket notation is only allowed in
initialization, not in assignment. Thanks for the quick response!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Signal Initialization Confusion Analog_Guy VHDL 1 08-12-2006 03:45 AM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments