Author Topic: Arrays interfering with each other  (Read 5300 times)

0 Members and 1 Guest are viewing this topic.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Arrays interfering with each other
« 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:

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:

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...
« Last Edit: December 24, 2012, 10:23:19 pm by epic7 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Arrays interfering with each other
« Reply #1 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

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Arrays interfering with each other
« Reply #2 on: December 25, 2012, 09:52:52 am »
Yep, shouldn't that be  for(lx = 0; lx < 10; lx++) ? :P
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Arrays interfering with each other
« Reply #3 on: December 25, 2012, 11:03:57 am »
Doesn't fix any problems, but nice catch nevertheless :P

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Arrays interfering with each other
« Reply #4 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?

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Arrays interfering with each other
« Reply #5 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
« Last Edit: December 25, 2012, 09:23:11 pm by epic7 »

Offline blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: Arrays interfering with each other
« Reply #6 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));
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Arrays interfering with each other
« Reply #7 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
« Last Edit: January 02, 2013, 09:49:00 pm by epic7 »