Question

I am trying to make a grid of blank spaces with 'x' dotted around randomly. I have managed to do this but now I want a separate function to print out the 2d array but am finding it hard to do so... any help will be great, thanks in advance.

int* createEnvironment(int width, int height, int numOfX)
{
    int i, j;

    char array[width][height];

    for (i = 0; i < width; i++)
    {
    for( j = 0; j < height; j++)
    {
         array[i][j] = '.'; 
    }
}

srand(time(NULL));

for(i=0; i<numOfX; i++)
{
    int row = ((rand() % (width-1)) + 0);
    int col = ((rand() % (height-1)) + 0);

    array[row][col] = 'x';

}


return array;
}

then print out 2d array...

void printEnvironment(int* environment, int width, int height)
{
  int i, j;

  for (i = 0; i < (height-1) ; i++)
 {
     printf("\n");

     for( j = 0; j < (width-1); j++)
     {
          printf("%c", environment[i][j]); 
     }
 }    
}

int main(int argc, char *argv[])
{  
int height = 20;
int width = 80;
int numOfX = 100;
int* environment;

environment = createEnvironment(width, height , numOfX);
printEnvironment(environment, width, height);


system("PAUSE");    
return 0;
}
Was it helpful?

Solution

(It looks like you have a stray }, but that's not relevant to the answer)

In your function int* createEnvironment(int width, int height, int numOfX), when you do char array[width][height];, you allocate a width x height block of memory in the stack. Your then use a few for loops that fill in the X's in certain spots. That all looks fine.

The problem is that array is allocated on the stack. Once createEnvironment returns, all of the variables that were placed on the stack will no longer be valid, since those areas of memory might be used by subsequent function calls. An easier way to think about this is to consider where each variable 'lives' within the program. Global variables live in the program and can be accessed by anyone. Variables that live in main can be accessed in main, and in functions that are called by main (i.e. with pointers). However, if you call function A from main, which has some variable V declared, and then call function B from main, B cannot access V, since V only lived in main. The solution to this problem would be to pass V back to main to be sent to B.

Fortunately, the C standard library provides malloc, which allocates memory off the heap, not the stack. When you allocate memory in the heap, it live in the heap regardless of which context it is created from. If function A creates variable V in the heap with malloc, then V can be accessed from main, B, or any other function, so long as a pointer to V is provided. This is also where memory leaks come from; when space is allocated from the heap, if it is not freed with free, then it stays allocated until the program ends.

The short answer is to replace the line char array[width][height]; with char * array = malloc(width * height * sizeof(char));. Not that the sizeof isn't strictly necessary since a char is one byte in size, but that would need to be changed if you changed char to some other type.

You'll also need to update the way you access a sub-element of the array. Since the width and height of the array won't be known if it's in the heap, you'll have to instead provide an offset from array, which should give you a clue how arrays work in C to begin with. I'll leave that for you to figure out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top