Like Tree3Likes
  • 1 Post By oogabooga
  • 2 Post By CornedBee

struct within a struct, odd behavior accessing variables

This is a discussion on struct within a struct, odd behavior accessing variables within the C++ Programming forums, part of the General Programming Boards category; I have a struct solar_system, and a struct planet. I want to create solar_system test_system, which should then contain planet ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    7

    struct within a struct, odd behavior accessing variables

    I have a struct solar_system, and a struct planet. I want to create solar_system test_system, which should then contain planet planets, then assign a string to test_system.planets[1 through max_planets].name, and then view the name of each planet.

    Heres the relevant code

    Code:
    struct planet
    {
        char name[];              
        int solar_system_position;  
    };
    
    struct solar_system
    {
        char name[];         
        int max_planets;    
        planet planets[];
        int n;
    
        solar_system() 
        {
            generate_random_name(name);
            max_planets=rand()%9+1;
            n=1;
            while(n<=max_planets)
            {
                generate_random_name(planets[n].name);
                planets[n].solar_system_position=n;
                n++;
            }
        }
    };
    
    void generate_random_name(char* name)
    {
        int n = rand()%3+1;
        switch(n)
        {
            case 1: strcpy(name,"one");break;
            case 2: strcpy(name,"two");break;
            case 3: strcpy(name,"three");break;
            case 4: strcpy(name,"four");break;
            default: mvprintw(0,0,"INVALID NAME GENERATION");break;
        }
    }
    The issue comes up when I try to access test_system.planets[n].name
    For whatever reason instead of printing "one" or "two" etc it prints "^A" "^B" and so on. If I add a line within solar_system() to print planets[n].name as it is generated everything is fine; it prints "one" and "two" and so on as would be expected. The issue only seems to come up when I try to access the name from outside of solar_system()

    I hope I explained the situation well enough, if more information is needed I'd be happy to provide it. I am still sort of a newbie so the code is probably not the prettiest but any help with this particular issue would be appreciated.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your main problem is that you're not setting aside any space to hold name or planets. You should use a string for name and either statically or dynamically allocate space for planets.

    rand()%3+1 gives a number between 1 and 3. Why not just use rand()%4 and do cases 0 to 3.

    You should use a for loop instead of a while loop i n solar_system's constructor (since that's exactly what a for loop is for).
    iMalc likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    All these arrays without size shouldn't even compile. Turn up the warning level on your compiler, and if it's MinGW, try enabling the "pedantic" option.

    Then throw your code away and rewrite it using std::string and std::vector.
    Salem and Elysia like this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange Struct behavior
    By masilva in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 02:37 PM
  2. Accessing a struct.
    By brack in forum C Programming
    Replies: 7
    Last Post: 09-04-2010, 02:00 AM
  3. accessing struct element from another struct
    By creek23 in forum C Programming
    Replies: 10
    Last Post: 06-24-2010, 03:56 AM
  4. Accessing a struct within a struct
    By Bladactania in forum C Programming
    Replies: 9
    Last Post: 04-21-2009, 09:06 AM
  5. accessing a struct with ->
    By Anddos in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21