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.
Related
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.
I'm trying to implement a coordinate system for a player in a game.
I have a struct
typedef struct player {
int playerPosition[1][5];
}
I create a pointer to that struct
struct* player playerPtr;
struct player playerOne;
playerPtr = &playerOne;
If during the game, I want to update the coordinate of the player to position [1,2].
I get errors when I use playerPtr->playerPosition=[1][2];
What's the correct way of doing this?
As written, you could do this:
playerPtr->playerPosition[0][2] = 100;
But probably you have an error here:
int playerPosition[1][5];
Because it rarely makes sense to have an array of size 1. Did you really mean for playerPosition to be a 1x5 array of ints? It looks like you wanted something like this:
struct position {
int x, y;
};
struct player {
position playerPosition;
};
Then you would do this:
playerPtr->playerPosition.x = 1;
playerPtr->playerPosition.y = 2;
playerPtr->playerPosition=[1][2]; will give you error (syntactically wrong)
you are not specifying the array index at which the data is to be stored, also you cant store data by that way in C.
correct way would be:
playerPtr->playerPosition[0][0] = 1;
playerPtr->playerPosition[0][1] = 2;
.
.
.
playerPtr->playerPosition[9][0] = 19;
playerPtr->playerPosition[9][1] = 20;
which is valid if you declare your array like this:
int playerPosition[10][2];
which will allow you to store ten coordinates.
2Dimentional arrays such as array[1][10] are same as array[10] (for usage, I am not certain about memory allocation, 2D array might require more memory)
I think you could use different but easier approach to this problem:
typedef struct position{
int x, y;
float refpos; //position from some reference point (like healing circle)
}position;
typedef struct player{
char name[20];
int health, attack, defense; //can be float too
position coord[20];
}player;
player player1, *playerPtr;
playerPtr = &player1;
playerPtr->position[0].x = 3;
playerPtr->position[0].y = 4;
playerPtr->position[0].refpos = 5; //Pythagorean triplet wrt origin (0,0)
Prost !
I was wondering, in Processing, how to include an array as part of an object. Say I have an Object named "Node" and want it to contain a list of all the IDs of other nodes it's connected to. Please note that the length of this list can be variable. That is, one node can be connected to two or seven different other nodes. And how would I access that particular array within that object?
Here's some code I'm working with:
void setup(){
size(200,200);
Node node1 = new Node(color(255,0,0),40,80,2,0,.5,5,5,0);
int neighbor = 6;
node1.neighbors.add(neighbor);
}
void draw(){
}
class Node {
Set<Node> neighbors;
color c;
float xpos;
float ypos;
float xspeed;
float yspeed;
float damp;
float ForceX;
float ForceY;
int id;
// The Constructor is defined with arguments.
Node(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed, float tempDamp, float tempForceX, float tempForceY, int id_temp) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
yspeed = tempYspeed;
damp = tempDamp;
ForceX = tempForceX;
ForceY = tempForceY;
id = id_temp;
neighbors = new HashSet<Node>();
}
}
Thanks!
Arrays can be included in a class just as any other var:
class Node{
int[] array;
Node (int[] _array){
array = _array;
}
/// all he stuff
}
But i think an object can't be "aware" of others objects of the same type. Maybe you need an other class Nodes, that takes an array of Node at constructor, or an ArrayList as the size must be variable. Or i'm afraid you have to handle the set of Node in draw().
You're describing a graph. A graph class should look like this:
class Node {
Set<Node> neighbors = new HashSet<Node>();
}
You may want to create methods such as addNeighbor(Node neighbor), isNeighbor(Node x) { return neighbors.contains(x); }, etc.
You described ids, but this method is more efficient assuming you have to load the other nodes in memory. The Set here containers references (e.g. C++ pointers) to other nodes.
You could also do a Set if you want ids and only integers.
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.
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.