Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: epic7 on December 24, 2012, 10:02:06 pm

Title: Arrays interfering with each other
Post by: epic7 on December 24, 2012, 10:02:06 pm
I'm experiencing some weird error... and my best guess would be that these two arrays are messing up each other.

So I have two portions of code.
This one:

   sprintf(recipes[18].name, "Stock ");
   recipes[18].power = 0;


And this one
               
   int invmenu[] ={352, 352, 352, 352, 352, 352, 352, 352, 352, 352, //352 == bedrock
               352, COAL, IRON, GOLD, PLATINUM, SAPPHIRE, EMERALD, RUBY, DIAMOND, 352,
               352, 0, 0, 0, 0, 0, 0, 0, 0, 352,  //0 == stone
               352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
               352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
               352, 0, 0, AIR, 0, 0, 0, 0, 0, 352,
               352, 352, 352, 352, 352, 352, 352, 352, 352, 352};


I then display the invmenu array (which contains tilemap data), and then output values from the recipe array.


for(ly = 0; ly <= 7; ly++)
{
   for(lx = 0; lx <= 10; lx++)
   {
      tile.x = lx*32;
      tile.y = ly*32+16;
      tile.offset_x = invmenu[ly*10+lx];
      if(tile.offset_x == AIR)
      {
         //stuff that replaces air with the "STOCK" tile
      }
      drawImageSubrect(&tile, s);
      tile.offset_y = 0;
   }      
}

sprintf(GUI, "%sDrill", recipes[18].name);
drawStr(133, 116, GUI, 1, 1, green, s);


The result is this:
(https://dl.dropbox.com/u/93019118/recipes1.png)
The invmenu[] data is fine, but the data from recipes[] is messed up.
Now when I put the portion of code with invmenu[] before the part with recipes[], it displays this:
(https://dl.dropbox.com/u/93019118/inv1.png)
So this time, the recipes[] data outputs fine, but invmenu[] is messed up.

So it seems that one array messes up the other...
When the part with invmenu[] comes first, only recipes[] works.
When the part with recipes[] comes first, only invmenu[] works :P

Commenting out the code between the assigning and the output doesn't get rid of this weird effect...
Title: Re: Arrays interfering with each other
Post by: Vogtinator on December 25, 2012, 08:33:14 am
Quote
for(ly = 0; ly <= 7; ly++)
{
   for(lx = 0; lx <= 10; lx++)
The last run would be: ly = 7, lx = 10.
invmenu[ly*10+lx] = invmenu[80].
In Java you would get an ArrayOutOfBoundsException :D
Title: Re: Arrays interfering with each other
Post by: Adriweb on December 25, 2012, 09:52:52 am
Yep, shouldn't that be  for(lx = 0; lx < 10; lx++) ? :P
Title: Re: Arrays interfering with each other
Post by: epic7 on December 25, 2012, 11:03:57 am
Doesn't fix any problems, but nice catch nevertheless :P
Title: Re: Arrays interfering with each other
Post by: Vogtinator on December 25, 2012, 11:07:20 am
Try to allocate them dynamically (malloc).
Does it make any differences if you allocate invmenu before recipes and vice versa?
Title: Re: Arrays interfering with each other
Post by: epic7 on December 25, 2012, 09:23:07 pm
How do I do that with arrays?
Sorry, I dove into making this game without that much C knowledge :P
Title: Re: Arrays interfering with each other
Post by: blue_bear_94 on December 25, 2012, 09:39:25 pm
You can declare a pointer variable, then assign the return value from malloc to that variable.
Code: [Select]
int* inventory=malloc(70*sizeof(int));
Title: Re: Arrays interfering with each other
Post by: epic7 on January 02, 2013, 09:48:52 pm
Finally got around to trying this :P

I put invmenu first, and then stored it into a pointer var, and that worked :D