将2D数组从函数返回到主函数

时间:2019-06-06 20:56:02

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

我正在编写《人生游戏》的代码,并且按照我的职责规定,我不应该使用指针。

为了计算每个像元的高度,在这种情况下,像元是我2D数组上的坐标,我编写了一个遍历所有行和列并计算每个像元有多少个ALIVE邻居的函数。最大值为8。

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

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

layout = Layout(;scene=attr(;xaxis=attr(;range=[-1, 8])))

下面是我遇到的问题:

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

如果我初始化一个有一些ALIVE细胞的20x20字段。

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

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

2 个答案:

答案 0 :(得分:2)

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

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

MyStruct func()
{
    MyStruct my_struct;

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

    return my_struct;
}

答案 1 :(得分:0)

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

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或一元&运算符的操作数,或者是用于初始化声明中的字符数组的字符串文字,否则 expression 为类型“ T的N元素数组将被转换(“衰减”)为类型“指向T的指针”的表达式,该表达式的值将是第一个元素的地址的数组。

因此,当您调用CellNeighbor( rows, cols, field, counting )时,表达式 field将从类型为rows的元素cols的元素数组-转换为cell”(cell[rows][cols])键入“指向cols的{​​{1}}-元素数组的指针”(cell)。类似地,表达式cell (*)[cols]从类型“ counting-元素数组rows-元素数组cols”(int)转换为“指向{的指针{1}}个元素数组,int [rows][cols]”(cols)。

这意味着int正在接收int (*)[cols]CellNeighbor pointer 值,而不是实际的数组。

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

如果您的分配是认真对待“没有指针”的,例如“完全没有指针,甚至数组表达式衰减的结果甚至没有隐式指针”,那么您就不能将数组表达式作为函数参数,句点传递。我很难过,很难相信这就是您的老师想要的。