Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 141841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:50:28+00:00 2026-05-11T07:50:28+00:00

I have created a couple of different structures in a program. I now have

  • 0

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 🙂 !!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:50:29+00:00Added an answer on May 11, 2026 at 7:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 276k
  • Answers 276k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the end I decided to settle on Ruby-GNOME2. Seemed… May 13, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer I think the code you want is this: begin select… May 13, 2026 at 2:46 pm
  • Editorial Team
    Editorial Team added an answer If you want to grab the string after the hash:… May 13, 2026 at 2:46 pm

Related Questions

I have a couple of projects with different release cycles sitting in my SVN
this is my first post on this website, but I'm all the time getting
I'm wondering what are the recommended ways to handle situations where, in memory managed
I am a fairly new MySQL developer and am starting on a project that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.