Thread: struct within a struct, odd behavior accessing variables

  1. #1
    Registered User
    Join Date
    Aug 2008

    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

    struct planet
        char name[];              
        int solar_system_position;  
    struct solar_system
        char name[];         
        int max_planets;    
        planet planets[];
        int n;
    void generate_random_name(char* name)
        int n = rand()%3+1;
            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
    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).
    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
    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.
    All the buzzt!

    "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, 01:37 PM
  2. Accessing a struct.
    By brack in forum C Programming
    Replies: 7
    Last Post: 09-04-2010, 01:00 AM
  3. accessing struct element from another struct
    By creek23 in forum C Programming
    Replies: 10
    Last Post: 06-24-2010, 02:56 AM
  4. Accessing a struct within a struct
    By Bladactania in forum C Programming
    Replies: 9
    Last Post: 04-21-2009, 08:06 AM
  5. accessing a struct with ->
    By Anddos in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 03:28 PM