Array/Struct Initialization Confusion

Q

quadraticformula

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
 
J

Jerry Coffin

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.
 
Q

quadraticformula

So what I would call the bracket notation is only allowed in
initialization, not in assignment. Thanks for the quick response!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top