简体   繁体   English

在一个函数中将二维数组从一个函数传递给另一个函数?

[英]passing 2d arrays from one function to another in c?

I am trying to make a grid of blank spaces with 'x' dotted around randomly. 我正在尝试使随机带有'x'的空格组成网格。 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. 我已经设法做到了,但是现在我想要一个单独的函数来打印2d数组,但发现这样做很难...任何帮助都会很大,在此先感谢您。

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;
}

(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]; 在你的函数int* createEnvironment(int width, int height, int numOfX) ,当你做char array[width][height]; , you allocate a width x height block of memory in the stack. ,您可以在堆栈中分配一个宽x高的内存块。 Your then use a few for loops that fill in the X's in certain spots. 然后,您可以使用一些for循环在某些位置填充X。 That all looks fine. 一切看起来都很好。

The problem is that array is allocated on the stack. 问题在于array是在堆栈上分配的。 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. 一旦createEnvironment返回,放置在堆栈上的所有变量将不再有效,因为随后的函数调用可能会使用这些内存区域。 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 (ie with pointers). main中存在的变量可以在main中以及在main调用的函数中(即使用指针)进行访问。 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. 但是,如果从main调用函数A(已声明了一些变量V),然后从main调用函数B,则B无法访问V,因为V仅存在于main中。 The solution to this problem would be to pass V back to main to be sent to B. 解决此问题的方法是将V传递回main以发送给B。

Fortunately, the C standard library provides malloc , which allocates memory off the heap, not the stack. 幸运的是,C标准库提供了malloc ,它从堆而不是堆栈中分配内存。 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. 如果函数A使用malloc在堆中创建变量V,则只要提供指向V的指针,就可以从main,B或任何其他函数访问V。 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. 从堆分配空间时,如果未使用free释放free ,则它将保持分配状态,直到程序结束。

The short answer is to replace the line char array[width][height]; 简短的答案是替换行char array[width][height]; with char * array = malloc(width * height * sizeof(char)); 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. 并不是因为char的大小为1个字节,所以不一定非要sizeof ,但是如果将char更改为其他类型,则需要更改它。

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. 由于无法知道数组的宽度和高度是否在堆中,因此您必须改为提供数组的偏移量,这应该为您提供了数组在C语言中如何开始工作的线索。 I'll leave that for you to figure out. 我会留给您找出答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM