C/C++ structs vs arrays question

Started by
7 comments, last by SiCrane 18 years, 8 months ago
Hello everyone, I have a C/C++ question relating to structs and arrays. The context is this: there's a DirectX tutorial I've been following, and in one portion the author makes a big array of custom-defined vertex structs. When declaring this array of vertices, he also initializes the value of each vertex in array notation, eg: struct CUSTOM_VERT { float x, y, z; float u, v; } CUSTOM_VERT myArray[] = { {1.0, 2.0, 3.0, 0.0, 0.0}, {1.0, 2.0, 3.0, 0.0, 0.0}, ..... ..... }; When I saw it, it puzzled me. I didn't know you could initialize structs in the the same way you can initialize arrays. In the end, he memcpy's the array into his unlocked vertex buffer, and then locks the buffer again. It seems to work just fine. The one thing I noticed is, his custom vertex struct is just a bunch of floats. It makes sense that you can treat a big array of structs like a 2D array if they all have the same datatype, because then a struct is pretty much just an array anyhow. But how far does this go? Could I do, for example: CUSTOM_VERT myVert = {1.0, 1.0, 1.0, 0.0, 0.0}; myVert[4] = 1.0; And what if all of the members in my struct were different types, could I still initialize them in the array notation? If anyone could tell me how much I can treat structs like arrays, I'd greatly appreciate it. Thanks! Mike
Advertisement
The standard actually calls the notation an aggregate initializer list. Arrays are aggregates as are some structs. When you use an aggergate initializer on a struct, the first element in the aggregate gets mapped to the first variable, the second element to the secong variable in the struct, etc. This works wether or not the struct is all a single kind of variable or a mix of different variable types.
Ohhh, very cool.

So the positions in the aggregate have nothing to do with what byte you're on in memory allocated to the variable, just the position in which the enumerated elements occur?

Pretty much. Oh, and one word of warning, extra {}'s inside the aggregates are sort of ignored. So in this code:
struct Inner {  int a;  double b;};struct Outer {  Inner c;  short d;  Inner e;};  Outer f  = { 1, 2, 3, 4, 5 };  Outer g  = { {1, 2}, 3, {4, 5} };  Outer h  = { 1, 2, 3, {4, 5} };

f, g and h are all initialized identically.
WHOA.

Really? The inner braces are just there as an optional visual aid? That's scary.

One last question though; do these rules only apply to this declaration aggregate proccess, or does it work for indexing too? Can I use [index] on a struct to reference members?
No, you can't use [] on a struct unless you define a overload for operator[] for the struct.
Word. Thank you kindly.

Mike
Quote:Original post by SiCrane
Arrays are aggregates as are some structs.


The set which qualify as POD structs, I assume?
No the set of structures which qualifies as aggregates not the same as the set of POD structs. A class can be aggregate initialized if it has no user defined constructors, no private or protected non-static data, no base classes and no virtual functions.

This topic is closed to new replies.

Advertisement