Adding Values to Pointer Arrays
Hey, I am trying to reprogram my code into using Pointer Arrays, versus my old system which used an Array but in a different way.
Before I defined my Class Array like this
CSECTOR* cSector[MAX_SECTORS];
for (DWORD c=0; cNumPlanets = rand() % 4;
// Set the stats for the different Planets
for (c2=0; c2NumPlanets;c2++)
{
// Set our Planet Scale from .5 to 1.0
cSector->cPlanets[c2].PlanetSize = float((rand() % 22)/10);
// Set the Planet population
cSector->cPlanets[c2].Population = 0;
// Set the Resouce Reserves
cSector->cPlanets[c2].OreReserves = 0;
cSector->cPlanets[c2].FuelReserves = 0;
cSector->cPlanets[c2].FoodReserves = 0;
// Set the Planet Type
cSector->cPlanets[c2].ePlanetType = PLANETTYPE_ENUM(rand() % 8);
}
}
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
I think it the problem lies in that I don''t reset it before I use it. How do I reset the array. For instance, the array has 100 values. How do I make it 0 without using a for loop and decrementing through it?
Thanks ahead of time!
Thanks ahead of time!
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
I'm not quite sure I understand what you're trying to do here, but either way I'll try and explain what I think you want .
If you what to use an array of pointers you should do something like:
CSECTOR **cSector = new CSECTOR *[MAX_SECTORS];
for(int i = 0; i < MAX_SECTORS; i++)
{
if(use(i)) // do we want to initialize this pointer?
{
cSector = new CSECTOR;<br> cSector->NumPlanets = …;<br> }<br> else<br> {<br> // nah, let this point to NULL<br> cSector = NULL;<br> }<br>}<br><br>/Andreas<br><br>Edited by - amag on January 8, 2001 5:51:01 AM
If you what to use an array of pointers you should do something like:
CSECTOR **cSector = new CSECTOR *[MAX_SECTORS];
for(int i = 0; i < MAX_SECTORS; i++)
{
if(use(i)) // do we want to initialize this pointer?
{
cSector = new CSECTOR;<br> cSector->NumPlanets = …;<br> }<br> else<br> {<br> // nah, let this point to NULL<br> cSector = NULL;<br> }<br>}<br><br>/Andreas<br><br>Edited by - amag on January 8, 2001 5:51:01 AM
I don't quite understand what you mean by 'Pointer-array', but I believe I fixed your problem with getting erraneous and random values in your data.
I think that should work. You did some strange things in your code, like accessing cPlanets without allocating them and incrementing the cSector-pointer outside of the loop...
You might have to click quote on this message to see exactly what I've written.
Good luck!
[EDIT]: Fixed code formatting
/Ksero
Mad Keith the V?? There are five of them??
Edited by - Ksero on January 8, 2001 6:00:42 AM
#define MAX_SECTORS 100CSECTOR* cSector;cSector = new CSECTOR[MAX_SECTORS]; for (DWORD c=0; c<MAX_SECTORS; c++) { srand((unsigned)time(NULL)); cSector[c].NumPlanets = rand() % 4; //Allocate the cPlanets cSector[c].cPlanets = new CPLANET[cSector[c].NumPlanets] // Set the stats for the different Planets for (c2=0; c2<cSector->NumPlanets;c2++) { // Set our Planet Scale from .5 to 1.0 cSector[c].cPlanets[c2].PlanetSize = float(0.5 + (rand() % 6)/10); // Set the Planet population cSector[c].cPlanets[c2].Population = 0; // Set the Resouce Reserves cSector[c].cPlanets[c2].OreReserves = 0; cSector[c].cPlanets[c2].FuelReserves = 0; cSector[c].cPlanets[c2].FoodReserves = 0; // Set the Planet Type cSector[c].cPlanets[c2].ePlanetType = PLANETTYPE_ENUM(rand() % 8); }}
I think that should work. You did some strange things in your code, like accessing cPlanets without allocating them and incrementing the cSector-pointer outside of the loop...
You might have to click quote on this message to see exactly what I've written.
Good luck!
[EDIT]: Fixed code formatting
/Ksero
Mad Keith the V?? There are five of them??
Edited by - Ksero on January 8, 2001 6:00:42 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement