Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize nested structures in C++?

I have created a couple of different structures in a program. I now have a structure with nested structures however I cannot work out how to initialize them correctly. The structures are listed below.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};

I then attempt to intialse a newly created Player struct called player.

//Creates player, usings vectors copy and iterator constructors
Player player = { 
vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ), //xcords of player
vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ), //ycoords of playe
box.ymax = 5;               //create bounding box
box.ymin = 1;
box.xmax = 5;
box.xmin = 1;
1,1,1,                      //red, green, blue
0.0f,0.0f,                  //r_leg,l_leg
{4,4,4},                    //number points per polygon
true,false};                //up, down

This causes several different errors, concerning box. Stating the box has no clear identifier and missing struct or syntax before '.'.

I then tried just to create a Player struct and initialise it's members as follows:

Player bob;
bob.r_leg = 1;

But this causes more errors, as the compiler thinks bob has no identifier or is missing some syntax.

I googled the problem but I did not find any articles showing me how to initalise many different members of nested structures within the (parent) structure. Any help on this subject would be greatly appreciated :-) !!!

like image 516
binarycreations Avatar asked Mar 10 '09 09:03

binarycreations


People also ask

How do you initialize a structure in C?

Structure members can be initialized using curly braces '{}'. For example, following is a valid initialization.

How do you create a nested structure?

A structure inside another structure is called nested structure. Consider the following example, struct emp{ int eno; char ename[30]; float sal; float da; float hra; float ea; }e; All the items comes under allowances can be grouped together and declared under a sub – structure as shown below.

How do you initialize a structure?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).

Is nested structure possible in C?

A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.


2 Answers

You initialize it normally with { ... }:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  5, 1, 5, 1, 5, 1, 5, 1, 
  1.0f,1.0f,1.0f,                         //red, green, blue
  0.0f,0.0f,                              //r_leg,l_leg
  {4,4,4},                                //number points per polygon
  true,false
};   

Now, that is using "brace elision". Some compilers warn for that, even though it is completely standard, because it could confuse readers. Better you add braces, so it becomes clear what is initialized where:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  { { 5, 1 }, { 5, 1 }, { 5, 1 }, { 5, 1 } }, 
  1.0f, 1.0f, 1.0f,               //red, green, blue
  0.0f, 0.0f,                     //r_leg,l_leg
  { 4, 4, 4 },                    //number points per polygon
  true, false
};

If you only want to initialize the x member of the points, you can do so by omitting the other initializer. Remaining elements in aggregates (arrays, structs) will be value initialized to the "right" values - so, a NULL for pointers, a false for bool, zero for ints and so on, and using the constructor for user defined types, roughly. The row initializing the points looks like this then

{ { 5 }, { 5 }, { 5 }, { 5 } }, 

Now you could see the danger of using brace elision. If you add some member to your struct, all the initializers are "shifted apart" their actual elements they should initialize, and they could hit other elements accidentally. So you better always use braces where appropriate.

Consider using constructors though. I've just completed your code showing how you would do it using brace enclosed initializers.

like image 79
Johannes Schaub - litb Avatar answered Oct 01 '22 07:10

Johannes Schaub - litb


You can add default values to a structure like so:

struct Point{
   Point() : x(0), y(0)
     { };

   float x;
   float y;
};

or

struct Point{
   Point() 
     { 
       x = 0;
       y = 0;
     };

   float x;
   float y;
};

For adding those values during construction, add parameters to constructors like this:

struct Point{
   Point(float x, float y) 
     { 
       this->x = x;
       this->y = y;
     };

   float x;
   float y;
};

and instantiate them with these:

Point Pos(10, 10);
Point *Pos = new Point(10, 10);

This also works for your other data structures.

like image 43
MrValdez Avatar answered Oct 01 '22 07:10

MrValdez