Skip to main content
GameDev.net gamedev.net
🔒 Locked

Initializing structs

Started by nickwinters Nov 1, 2004 at 6:17 PM 3 replies 450+ views
Original Post
nickwinters
nickwinters
I have the following struct:
struct Vertex
{
	float x, y, z;
	float nx, ny, nz;
	float tu, tv;
};
What's wrong with the following instantiations?
Vertex v[3];
v[0]={-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f};
v[1]={0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f};
v[2]={1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
Thanks. -Nick
SiCrane
SiCrane
Aggregate initializers (the curly brace style initializers) can only be used when declaring the variable. You'd need to change the declaration to something like:
  Vertex v[3] = {    {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},    {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f},    {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f} };

if you want to use the aggregate initializer form.
flangazor
flangazor
Original post by SiCrane
Aggregate initializers (the curly brace style initializers) can only be used when declaring the variable. You'd need to change the declaration to something like:
  Vertex v[3] = {    {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},    {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f},    {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f} };

if you want to use the aggregate initializer form.[/quote]
  enum  {    X=0,    Y,    Z,    NUM_CORDS  }  Vertex v[NUM_COORDS] = {    {-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},    {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f},    {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f} };
Self-documenting. Assumes that this is what the 3 was for -otherwise feel free to rewrite my stub.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.