【问题标题】:Returning a 2D array from function to main function将二维数组从函数返回到主函数
【发布时间】:2019-06-06 20:56:02
【问题描述】:

我正在为 Game of Life 编写代码,按照我的分配规定,我不应该使用指针。

为了计算每个单元格的邻居数量,这里的单元格是我的二维数组上的坐标,我编写了一个函数,它遍历所有行和列并计算每个单元格有多少个 ALIVE 邻居。最大值为 8。

但是我不知道如何返回我的数组,它将相邻单元格的数量存储在 20x20 数组中。

下面是整个代码。请注意,有些部分未完成,因为我正在补充给我的模板。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'

/* Declaration of data structure */
typedef struct{
  char current;
  char next;
} cell;

/* Declaration of functions */
void initField(const int rows, const int cols, cell field[rows][cols]);
void loadGlider(const int rows, const int cols, cell field[rows][cols]);
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
void loadRandom(const int rows, const int cols, cell field[rows][cols]);
void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void printWorld(const int rows, const int cols, cell field[rows][cols]);
int CellNeighbour(const int rows, const int cols, cell field[rows][cols]);



/* Function:    main
* Description: Start and run games, interact with the user.
* Input:       About what initial structure and whether to step or exit.
* Output:      Information to the user, and the game field in each step.
*/

int main(void) {

  const int rows = 20;
  const int cols = 20;
  cell field[rows][cols];
  int counting[rows][cols];

  initField(rows,cols, field);
  printWorld(rows,cols,field);
  CellNeighbour(rows,cols,field);//test



    for (int i = 0; i < rows; i++){
        for (int j = 0; j < cols; j++){
            printf("%d ", counting[i][j]);
        }
        printf("\n");
    }




  return 0;
}


/* Function:    initField
* Description: Initialize all the cells to dead, then asks the user about
*              which structure to load, and finally load the structure.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void initField(const int rows, const int cols, cell field[rows][cols]) {

  for (int r = 0 ; r < rows ; r++) {
    for (int c = 0 ; c < cols ; c++) {
      field[r][c].current = DEAD;
    }
  }

  printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
  printf("or [C]ustom): ");

  int ch = getchar();

  /* Ignore following newline */
  if (ch != '\n') {
    getchar();
  }

  switch (ch) {
    case 'g':
    case 'G':
    loadGlider(rows, cols, field);
    break;
    case 's':
    case 'S':
    loadSemaphore(rows, cols, field);
    break;
    case 'r':
    case 'R':
    loadRandom(rows, cols, field);
    break;
    case 'c':
    case 'C':
    default:
    loadCustom(rows, cols, field);
    break;
  }
}


/* Function:    loadGlider
* Description: Inserts a glider into the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadGlider(const int rows, const int cols, cell field[rows][cols]) {

  field[0][1].current = ALIVE;
  field[1][2].current = ALIVE;
  field[2][0].current = ALIVE;
  field[2][1].current = ALIVE;
  field[2][2].current = ALIVE;
}


/* Function:    loadSemaphore
* Description: Inserts a semaphore into the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {

  field[8][1].current = ALIVE;
  field[8][2].current = ALIVE;
  field[8][3].current = ALIVE;
}


/* Function:    loadRandom
* Description: Inserts a random structure into the field.
* Input:       The field array and its size.
* Output:      The field array is updated. There is a 50 % chance that a cell
*              is alive.
*/

void loadRandom(const int rows, const int cols, cell field[rows][cols]) {

}


/* Function:    loadCustom
* Description: Lets the user specify a structure that then is inserted into
*              the field.
* Input:       The field array and its size.
* Output:      The field array is updated.
*/

void loadCustom(const int rows, const int cols, cell field[rows][cols]) {

  printf("Give custom format string: ");
  do {
    int r, c;
    scanf("%d,%d", &r, &c);
    field[r][c].current = ALIVE;
  } while (getchar() != '\n');
}
/* Function:    printWorld
* Description: Prints the current field
* Input:       The field array and its size.
* Output:      The field array is updated.
*/


void printWorld(const int rows, const int cols, cell field[rows][cols]){

  char c = '\n';

  while(c == '\n'){
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        printf("%c ", field[i][j].current);
      }
      printf("\n");
    }
    c = getchar();
    if(c != '\n'){
      break; // hoppa ut ur loopen till main funktionen
    }

  }
}

void evolve(const int rows,const int cols,cell field[rows][cols]){

  for(int i = 0;i<rows;i++){
    for(int j =0;j<cols;j++){
      if(field[rows][cols].current == ALIVE  && ArrayDatCorresponds2NmbofNeighb[rows][cols]<2){
      }
      if(field[rows][cols].current == ALIVE && ArrayDatCorresponds2NmbofNeighb[rows][cols] ==3 ||ArrayDatCorresponds2NmbofNeighb[rows][cols] ==2 ){
        field[rows][cols].next = ALIVE;
      }
      if(field[rows][cols].current == ALIVE && ArrayDatCorresponds2NmbofNeighb[rows][cols] >= 4 ){
        field[rows][cols].next = DEAD;
      }
      if(field[rows][cols].current == DEAD && ArrayDatCorresponds2NmbofNeighb[rows][cols] ==3){
        field[rows][cols].next = ALIVE;
      }
    }
  }



int CellNeighbour(const int rows, const int cols, cell field[rows][cols]){

  int i,j;
  int count =0;
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
  int StoreArray[rows][cols] =0;
 }
}

  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      if(field[rows-1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
    }
  }


  return StoreArray; 
}

以下是我遇到问题的功能:

int CellNeighbour(const int rows, const int cols, cell field[rows][cols]){

  int i,j;
  int count =0;
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
  int StoreArray[rows][cols] =0;
 }
}

  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      if(field[rows-1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols-1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows+1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols+1].current == ALIVE){
        StoreArray[i][j]=count++;
      }
      if(field[rows-1][cols].current == ALIVE){
        StoreArray[i][j]=count++;
      }
    }
  }


  return StoreArray; 
}

如果我初始化一个 20x20 的字段,其中有一些 ALIVE 单元格。

然后我希望,在打印出一个 5x5(只是为了简单起见)计算每个单元格有多少邻居之后,一个看起来像这样的网格:

2 2 0 0 0
2 3 0 0 0
0 0 0 0 0
0 1 1 0 0
0 0 0 0 0

【问题讨论】:

  • 您不能从函数返回数组类型。为了满足您的分配条件,您必须将目标数组作为参数传递给您的函数(即int CellNeighbour(const int rows, const int cols, cell field[rows][cols], cell counts[rows][cols])
  • @JohnBode 这也需要指针。所以它也不起作用。
  • 执行此操作的正常方法是将指向数组的指针作为参数传递,正如 John Bode 所建议的那样。另一种方法是(呃)访问一个全局数组。
  • @NikosC.:那么他也不能将他的源数组传递给函数,如果真的是这样的话,我真的会质疑赋值的意图。
  • @JohnBode 他们确实不鼓励人们这样做。可能是因为他们有其他方法。我可以尝试电子邮件讲师,他们通常对这些东西并不坏。那么我将如何处理指针?在我的主函数中声明StoreArray 作为指针并将其传递给我的CellNeighbour 函数?

标签: c multidimensional-array conways-game-of-life


【解决方案1】:

您不能在 C 中返回数组。而且由于不允许使用指针,因此您必须将数组包装在结构中。示例:

typedef struct {
    int data[rows][cols];
} MyStruct;

MyStruct func()
{
    MyStruct my_struct;

    // Fill my_struct.data with what you need.
    // ...

    return my_struct;
}

【讨论】:

  • 这是个好主意,但需要对代码进行其他更改。目前,该函数采用数组维度的 const 参数,例如 CellNeighbour(const int rows, const int cols, cell field[rows][cols])。结构必须在其声明中定义维度,因此它不能接受这些参数。考虑到这一点,我建议的另一种方法是简单地在调用者中声明数组并将其传递给函数进行修改。
  • @ChrisRollins 您不能将数组传递给函数。您将指针传递给数组的第一个元素。所以这意味着你正在使用指针。反过来,这也意味着您已经在程序中使用了指针。你确定你没有误解你的任务吗?
  • 对,对不起。那么我认为数组维度不应该有争论。这些可以是全局常量。
  • @NikosC.Cheers 伙计。正如我对@JohnBode 所说。非常不鼓励指点,如果动机良好,我的讲师对于违背作业的某些部分并不那么严格。所以我现在可能会使用指针。然后我应该将StoreArray[rows][cols] 声明为我的主函数中的指针并将其传递给我的CellNeighbour 函数吗?
  • 如果您将指针传递给数组,则数组本身不需要声明为指针。它可以按照现在的方式声明。您可以将指向数组的指针传递给函数,以便访问该数组。
【解决方案2】:

正如我在评论中提到的,您不能从函数返回数组类型。 正确的方法是将目标数组作为参数传递:

const int rows = 20;
const int cols = 20;
cell field[rows][cols];
int counting[rows][cols];
...
CellNeighbor( rows, cols, field, counting );

然后您将 CellNeighbor 函数定义为:

void CellNeighbour(const int rows, const int cols, cell field[rows][cols], int counts[rows][cols])
{
  int i,j;
  int count =0;

  // you could probably replace the following with memset( counts, 0, rows * cols * sizeof counts[0][0]
  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      counts[i][i] =0;
    }
  }

  for( i =0;i<rows;i++){
    for( j = 0;j<cols;j++){
      if(field[i][j].current == ALIVE){
        counts[i][j]=count++;
      }
      if(field[i][j-1].current == ALIVE){
        counts[i][j]=count++;
      }
      ...
    }
  }
}

现在,您没有在任何地方显式使用指针,所以这应该满足赋值条件。

但是……

除非它是sizeof 或一元&amp; 运算符的操作数,或者是用于在声明中初始化字符数组的字符串文字,否则表达式 类型为“N- T" 的元素数组将被转换 ("decay") 为 "pointer to T" 类型的表达式,表达式的值将是数组第一个元素的地址。

所以当你调用CellNeighbor( rows, cols, field, counting )时,表达式field是从类型“rows-cols的元素数组-cell的元素数组”(cell[rows][cols] ) 键入“指向cols-cell 的元素数组的指针”(cell (*)[cols])。同样,表达式counting从类型“rows-元素数组cols-元素数组int”(int [rows][cols])转换为“指向cols-元素数组int的指针” (int (*)[cols])。

这意味着CellNeighbor 正在接收fieldcounting指针 值,而不是实际的数组。

在函数参数声明的上下文中,T a[N]T a[] 被“调整”为 T *a,因此声明 cell field[row][cols]int counts[row][cols] 在中被解释为 cell field (*)[cols]int counts (*)[cols] CellNeighbor 函数定义。

如果您的分配对“无指针”是认真的,如“根本没有指针,甚至没有作为数组表达式衰减结果的隐式指针”,那么您不能将数组表达式作为函数参数传递,句号。我真的很难相信这就是你的导师的意图。

【讨论】:

  • 我是新手。所以我可能没有意识到指针有多么庞大。但我认为他的意思是当变量被声明为指针以及我们取消引用它们时使用指针。但我知道我们可以将数组表达式作为函数参数传递。所以我可能是罪魁祸首
  • @AliasaZarownyPseudonymia:这是数组类型的一个特性——规则的存在是因为 Ritchie 想要保留 B 的数组语义(C 是从 B 派生的——是的,有一种语言叫做 B),但没有不想具体化那些语义所需的指针,所以在设计 C 语言时,他提出了数组表达式“衰减”为指针的规则。没有其他类型以这种方式表现。它令人困惑,这也是为什么 C 不是入门编程课程的糟糕选择的部分原因。
猜你喜欢
  • 2012-01-26
  • 2021-03-29
  • 2020-04-08
  • 1970-01-01
  • 2023-03-14
  • 2021-05-01
  • 2021-12-04
  • 2014-12-19
相关资源
最近更新 更多