Best way to handle array nested structs in C - c

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

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;
};

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.

Related

C structure that ends with an asterisk

I was looking at this example and I found out that there is the declaration
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = NULL;
What does this actually mean? Is it possible to create a structure which is also a pointer with the name front and it NULL...?
A struct is just another C type, as such, the variables it is used to define, may be created as normal instances, or pointers:
int a, *pA=NULL; //normal instance, pointer instance
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}sEdge, *front = NULL; //normal instance, pointer instance
And, as with any pointer variable, needs to be pointed to owned memory before it can be safely used: (examples)
int main(void)
{
// both variable types are handled the same way...
pA = &a; //point pointer variable to normal instance of `int a`
front = &sEdge;//point pointer `front` to instance of 'struct edge'
//allocate memory, resulting in assigned address with associated memory.
pA = malloc(sizeof(*pA));
front = malloc(sizeof(*front));
...
EDIT to answer question in comments:
This small example throws no error or warning. (Edit your question above, or better yet, post another question showing details of what you are seeing.)
struct edge
{
int x;
int y;
int weight;
struct edge *link;
}*front = '\0';
int main(void)
{
struct edge tree[10];
return 0;
}
It is a pointer to a struct and a declaration of a new type called struct edge.
Maybe that would put some more light, when you write:
struct edge
{
int x;
int y;
int weight;
struct edge *link;
};
You are saying: I'm creatig struct edge, which I will use to define objects of this struct by typing:
struct edge edgeObject;
But when you write:
struct edge
{
int x;
int y;
int weight;
struct edge *link;
} edgeObject;
You are saying: I'm creating struct edge and at the same time I'm defining edgeObject which is of type struct edge.
And this allows you to use that object directly as it is already defined:
edgeObject.x = 0;
So going back to your example you are saying: I'm creating structure edge and at the same I'm defining pointer to that struct front which is set to NULL.

How do you assign values from a linked list

I'm adding the next bit to this silly assignment of mine, and I'm modifying my old code now that I'm implementing these 'Linked List' things, but I can't figure out how you pull data out of them?
So, the software needs to pull the variables for whatever planet you're up to in the list, and assign the numbers to the variables, if that makes sense?
typedef struct Planet
{
char Planet_Name[30];
double Fuel;
double Velocity;
double Height;
double Gravity;
int Maximum_Thrust;
double Difficulty;
}Planet_t;
typedef struct PlanetNode
{
Planet_t* planet_Name;
struct PlanetNode* next;
}PlanetNode_t;
typedef struct PlanetList
{
PlanetNode_t* head;
int count;
}PlanetList_t;
(in main)
PlanetList_t* SolarSystem = calloc(1, sizeof(PlanetList_t));
printf("The Planets are (with their corresponding difficulty level)
Pluto[0], Moon[1], Mercury[2], Mars[3].....
double height = SolarSystem->head->Height;
double velocity = SolarSystem[PlanetNum]->Velocity;
double fuel = SolarSystem[PlanetNum]->Fuel;
double gravity = SolarSystem[PlanetNum]->Gravity;
double fuelBurn;
double difficulty = SolarSystem[PlanetNum]->Difficulty;
So basically, I need to figure out that last bit. Each iteration of the program changes the selected planet (linearly going through the planets), so the variables need to be renewed each time.

How to dynamically allocate a struct containing multiple arrays?

I'm trying to make a struct that contains another struct with multiple arrays. I need to dynamically allocate those arrays too, so I think I need another pointer still.
int arraysize;
typedef struct Array{
int *size = arraysize;
unsigned int val[*size];
unsigned int x[*size];
unsigned int y[*size];
} Array;
typedef struct Image{
int height;
int width;
int max;
Array *data;
} Image;
OK, so once I finally figure that out, I still need to figure out how to dynamically allocate that memory using malloc. I'm totally lost there too. Any help at all would be greatly appreciated.
EDIT: more clarification:
I'm using the arrays to store three pieces of information that are all connected. Think of a chessboard, you could say knight E4, which tells you that on the 4th column of row E, there is a knight. If you started this process at A1 and ended at K10 you'd have a full chessboard right? The image struct is analogous to the chessboard, the Array is analogous to a list of a bunch of squares that compose the chessboard and the contents of those squares. (E.g. A1 null A2 knight a3 bishop etc...) Unfortunately, I don't know what kind of board will be passed through, it might be a 3x7 board or a 9x2 board etc. So I need to dynamically allocate the memory for those possibilities. Once I have the memory allocated I need to store information about the location and the contents of all of the "squares." Then I need to let a program pass through the height of the board, width of the board and the list of contents and I'd be done the hard part.
What you actually meant was:
typedef struct data {
unsigned int x;
unsigned int y;
unsigned int val;
} Data;
typedef struct image {
int height;
int width;
int max;
Data* data;
} Image;
and somewhere:
Image i;
i.height = 10;
i.width = 20;
i.data = malloc(sizeof(Data) * i.width * i.height);
...
// one of the ways how to access Data at 2nd row, 3rd column:
*(i.data + i.width * 1 + 2).val = 7;
...
free(i.data);
i.data = NULL;
But what you actually need is some good book ;)

How do I null an array of pointers that is pointing to a Structure in C (not C++)

I'm currently trying to Null an array of pointers that point to a structure. Any help or documentation would be nice. I am a beginner so please be as clear as possible.
Here is an example of my code. Sorry if I don't have this listed correctly, it's my first posting.enter code here
#include "stdlib.h"
enum boxtype
{
Card,
Mask,
};
typedef struct
{
enum boxtype type;
int L;
int H;
int x;
int y;
int Area;
Float ManBox;
Float WomanBox;
}Boxes;
typedef struct
{
Boxes Info;
float Hight;
}Male;
typedef struct
{
Boxes Info;
int Size;
}Female;
void main()
{
Man Male[100];
Woman Female[100];
Boxes *Spaces[600]; //This is the array of pointers that needs to be nulled.
}
You can initialize an array with an initializer list like so:
Boxes *Spaces[600] = { NULL };
All of the elements in the array will be set to NULL.
if you initialize with calloc() the memory will be zero'ed before it is returned.
for(int i=0;i<600;i++)
{
*Space[i]=NULL;
}
the previous given answer was also right. If u don't understand the previous code then u can try this one.

Pointer to struct within different struct. C

I am currently working on a text based game in C and I'm having a problem altering values when certain events happen. Here is some of my data structure code:
typedef struct player {
int maxhealth;
int curhealth;
int in_combat;
monster c_enemy;
char *class;
char *condition;
rooms c_room;
inventory i;
stats stats;
} player;
Now, I think my problem is that I currently have c_room (Current Room) as a rooms, instead of a pointer to a rooms. This affects me later because I need to alter things like n_monsters within the struct rooms for the current room. However, when I modify it by doing p.c_rooms.n_monsters -= 1; I'm not sure it alters the actual value of n_monsters for the room that I should be referring to. I've tested this by leaving a room when n_monsters is 0, and then coming back to see that it's back at 1, the default value.
So yea, how would I point to right room?
Just:
typedef struct player {
int maxhealth;
int curhealth;
int in_combat;
monster c_enemy;
char *class;
char *condition;
rooms *c_room; // Like this?
inventory i;
stats stats;
} player;
// And then the assignment would look like:
c_room = *rooms[3]; <- an array of rooms for the dungeon in the game.
Assuming that c_room is a plain struct and not a pointer then you are right.
If you have
struct A {
int v;
};
struct B {
struct A a;
}
A a;
a.v = 3;
B b;
b.a = a;
This will actually copy the content of a inside B.a since they are assigned by value. They will be two different A, any modification to one of them won't be reflected on the other.
In your situation I would do something like:
struct Room {
// whatever
}
struct Room rooms[MAX_ROOMS];
struct Player {
struct Room *room;
}
Player p;
p.room = &rooms[index];
Now you will be able to correctly reference to room by p->room, it will be just a pointer to the actual room.

Resources