2

我正在用 C 编写一个简单的游戏。所以我试图将一些数据保存到我正确 malloc 的 2dim 数组中。问题是,当我想将写入的那个变量传递给另一个函数时。

int readMap(FILE *eingabe, map_t *map, config_t *configstruct, pacman_t *pacman)
{
  int i = 0, j = 0, k = 0, pac = 0;
  map->mapdesign = (char**) malloc(sizeof(char*) * map->height);
  do
  {
    for (i = 0; i < map->height; i++)
    {
      map->mapdesign[i] = (char*) malloc(sizeof(char) * (map->width + 1));
      for (j = 0; j < map->width; j++)
      {
        fscanf(eingabe, "%c", &map->mapdesign[i][j]);
        printf("%c", map->mapdesign[i][j]);
        if (map->mapdesign[i][j] == configstruct->ghost)
          map->ghostcount++;
        else if (map->mapdesign[i][j] == configstruct->foodtypes[0]
            || map->mapdesign[i][j] == configstruct->foodtypes[1])
        {
          map->foodcount++;
        }
        for (k = 0; k < PAC; k++)
        {
          if (map->mapdesign[i][j] == configstruct->pacman[k])
          {
            pacman->cordinate.x = j;
            pacman->cordinate.y = i;
            if (pac > 1)
              return -1;
            pac++;
          }
          k++;
        }
      }
    }
  } while (!feof(eingabe));
  return 0;
}

当我在函数本身中使用该 printf 时,它会打印出我想要的内容。然后我决定我想使用一个为我打印出来的函数,看起来像这样:

int renderMap(map_t *mapstr)
{
  int i = 0;
  clrscr();
  for (i = 0; i < mapstr->height; i++)
    puts(mapstr->mapdesign[i]);
  return 0;
}

上面的这个函数应该打印出:

糊盒 1

但它实际上打印了这个:

糊盒 2

提前致谢!

4

1 回答 1

1

我建议首先了解 printf 和 puts 的两个函数之间的差异,因为它们对字符终止的处理方式不同。那可能是错误。

或者发送相对完整的代码,以便两个函数可以独立运行,我将调试它并返回给您。

于 2015-01-15T16:38:57.013 回答