0

Hey everyone, I've been having some trouble in C, i'm using a variety of array nested structs in order to model a universe. Here is the struct code...

struct star  
{  
    int x;  
    int y;  
    int z; 
    int m;  
    char name[100];  
};  
struct colony  
{  
    int pop;  
};  
struct planet  
{  
    int x;  
    int y;  
    int z;  
    int m;  
    int colonized;  
    char name[100];  
    struct colony colony_member;  
};  
struct galaxy  
{  
    int x;  
    int y;  
    int z;  
    char name[100];  
    struct planet planet_member;  
    struct star star_member;  
}; 

Let's say I made 10 random galaxies with random values in the struct, how would I create 100 planets within that galaxy struct? I'm confused as how the best way to handle this would be, or even if structs if the way I want to go.

Thanks in advance!
-Devan

0

2 Answers 2

3

You can create a pointer and allow varied number of planets and stars to your galaxy!

struct galaxy
{
   int x;
   int y;
   int z;
   char name[100];
   struct planet *planet_member;
   struct star *star_member;
};
Sign up to request clarification or add additional context in comments.

3 Comments

My knowledge of pointers is not very good, could you explain a little more? I understand what a pointer is but i'm not sure what exactly to do with a struct pointer like that.
You'd also want to keep track of how many planet_member and star_member entries there are or you won't be able to free them or grow the arrays.
@pwnmonkey - time to read up on pointers mate! there are tons of tutorials on the net; this being one (pw1.netcom.com/~tjensen/ptr/pointers.htm).
2

You have several options available, starting with the easiest:

struct galaxy  
{  
  int x;  
  int y;  
  int z;  
  char name[100];  
  int number_of_planets;
  struct planet *planet_member;  // a pointer!
  struct star star_member;  
}; 

and to create a galaxy:

galaxy g;
g.number_of_planets = some_random_value
g.planet_member = malloc (sizeof (planet) * g.number_of_planets);
for (i = 0 ; i < g.number_of_planets ; ++i)
{
   g.planet_member [i].x = something
   g.planet_member [i].y = something
   g.planet_member [i].z = something
   // and so on for each planet
}

Don't forget, you need to free the memory you malloc, otherwise you'll get a memory leak.

You could use more complex data structures, like a linked list. So, your galaxy struct has a pointer to the first and last planet in the list. Each planet has a pointer to the next and previous planet in the list. So starting with the first planet and reading the next planet pointer you can process each planet in the list. Look up linked list on Google to find more information about it. It's a lot more work, and work that's been done many times already, which leads to....

...progressing to C++ where there's a standard library that can do all the fiddly housekeeping of linked lists and other data types for you. So, your structure would become:

struct galaxy  
{  
  int x;  
  int y;  
  int z;  
  std::string name;  
  std::vector <struct planet> planets; // vector is an array like type
  std::list <star> stars;  // list is a linked list
}; 

But, you could go further still and make galaxy a C++ class so that when you create one, it automatically creates the planets and stars in it, and when you get free it, the class automatically frees all the planets and stars it holds.

4 Comments

I'm not sure what i'm doing wrong, but i'm getting a "galaxy undeclared" error on "galaxy g;" I'm sure its just some stupid mistake of mine. Any idea though?
@pwnmonkey: try struct galaxy g; or, typedef struct _galaxy { /* contents of struct galaxy */ } galaxy; to make galaxy g; work.
However, it wont let me assign g or g.planet_member a name. "error: incompatible types when assigning to type ‘char[100]’ from type ‘char *’"
@pwnmonkey: we can't copy strings like that in C, you need to use strcpy, or better, strncpy: strncpy (g.name, source_name, 100); the 100 is the size of the destination array and is the maximum number of characters that can be copied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.