• Advertisement
Sign in to follow this  

Array not acting like it should (in C)

This topic is 3420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I'm bugging here. I'm trying to build an array that contains a list of all modifications I can make to a maze array. Unfortunately every time I add something to the array it overwrites all previous values of the array! I've stripped down the program so you can see just what's going wrong:
#include <curses.h>
#include <stdlib.h>
#include <time.h>

#define MAX 3
#define NUMBENDS 8

enum Direction {NONE, UP, LEFT, DOWN, RIGHT, END};
enum Direction board[MAX * MAX];
int start, end;

enum Direction bends[NUMBENDS][2][2][2] =       /* [type][before/after][y][x]*/
{
{{{ NONE, DOWN},{ NONE,  END}},{{ DOWN, LEFT},{RIGHT,  END}}},
{{{ NONE, NONE},{  END, LEFT}},{{ DOWN, LEFT},{  END,   UP}}},
{{{  END, NONE},{   UP, NONE}},{{  END, LEFT},{RIGHT,   UP}}},
{{{RIGHT,  END},{ NONE, NONE}},{{ DOWN,  END},{RIGHT,   UP}}},
{{{ NONE,  END},{ NONE,   UP}},{{RIGHT,  END},{   UP, LEFT}}},
{{{ NONE, NONE},{RIGHT,  END}},{{RIGHT, DOWN},{   UP,  END}}},
{{{ DOWN, NONE},{  END, NONE}},{{RIGHT, DOWN},{  END, LEFT}}},
{{{  END, LEFT},{ NONE, NONE}},{{ END, DOWN},{   UP, LEFT}}}
};
int step[4] = {-MAX, -1, 1, MAX};

int debug=0;

void init ()
{
    initscr ();
    noecho();
}

int change () 
{
  int xy, c, d, index, mods[512][0]; /* mods[index][location/type] */

       /* Now we make a list of all possible modifications. First the bends...*/
debug = 0;
    index = 0;
    for (xy = 0; xy < (MAX * MAX) - MAX; xy++) 
    {
      if ((xy + 1) % MAX == 0) continue;
      for (c = 0; c < NUMBENDS; c++) 
      {
        mods[index][0] = xy; mods[index][1] = c; index++;

debug++; for (d = 0; d < index; d++) 
mvprintw(debug++,40,"InBends%d:%d,%d",d,mods[d][0],mods[d][1]);refresh();
      }
    }
    getch();

  return 0;    /* None of the modifications worked, so back up and try again. */
}

void start_generating () 
{
  int c;

  for (c = 0; c < MAX * MAX; c++) board[c] = NONE;         /* Clear the board */
  start = rand() % (MAX * MAX);
  do {
    switch (rand() % 4) {
      case 0 : if (start - MAX >= 0) 
              {
                 board[start] = UP;
                 end = start - MAX;
              } break;
      case 1 : if (start % MAX) 
              {
                 board[start] = LEFT;
                 end = start - 1;
               } break;
      case 2 : if (start + MAX < MAX * MAX) 
              {
                 board[start] = DOWN;
                 end = start + MAX;
               } break;
      case 3 : if ((start + 1) % MAX) 
              {
                 board[start] = RIGHT;
                 end = start + 1;
               } break;
    }
  } while (board[start] == NONE);
  board[end] = END;
  change ();
}

int main () {
  init();
  getch();
  srand (time (NULL));
  start_generating ();
  return 0;
}
What I should be getting is a debug output that looks like:
InBends0:0,0

InBends0:0,0
InBends1:0,1

InBends0:0,0
InBends1:0,1
InBends1:0,2

InBends0:0,0
InBends1:0,1
InBends2:0,2
InBends3:0,3
ETC. What I am getting is:
InBends0:0,0

InBends0:0,1
InBends1:0,1

InBends0:0,2
InBends1:0,2
InBends2:0,2

InBends0:0,3
InBends1:0,3
InBends2:0,3
InBends3:0,3

InBends0:0,4
InBends1:0,4
InBends2:0,4
InBends3:0,4
InBends4:0,4

InBends0:0,5
InBends1:0,5
InBends2:0,5
InBends3:0,5
Why is it overwriting the rest of the array when it adds something?

Share this post


Link to post
Share on other sites
Advertisement
This array declaration looks extremely fishy:

int mods[512][0];

Why are you defining an array with one of its dimensions being 0?

Share this post


Link to post
Share on other sites
I only skimmed your code, but this seems a bit odd:

 mods[512][0]

How about actually giving the second dimension of the array a meaningful length? :p

Share this post


Link to post
Share on other sites
DUUUUUUUUUUUH! I kept expanding the first number but ... WHY! Thanks guys.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement