Jump to content
  • Advertisement
Sign in to follow this  
jujumbura

C/C++ structs vs arrays question

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
No, you can't use [] on a struct unless you define a overload for operator[] for the struct.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Arrays are aggregates as are some structs.


The set which qualify as POD structs, I assume?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!