Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Array/Struct Initialization Confusion (http://www.velocityreviews.com/forums/t620380-array-struct-initialization-confusion.html)

quadraticformula 06-16-2008 03:24 AM

Array/Struct Initialization Confusion
 
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

Jerry Coffin 06-16-2008 04:15 AM

Re: Array/Struct Initialization Confusion
 
In article <daafc171-591b-45af-a5d4-9eb4c651870b@
56g2000hsm.googlegroups.com>, dawnofdigital@gmail.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.

quadraticformula 06-16-2008 05:01 AM

Re: Array/Struct Initialization Confusion
 
So what I would call the bracket notation is only allowed in
initialization, not in assignment. Thanks for the quick response!


All times are GMT. The time now is 06:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.