【问题标题】:2D array in by reference通过引用输入的二维数组
【发布时间】:2015-01-12 21:06:36
【问题描述】:

我有这个……

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

//definições de constantes usadas para agilizar a implementação.
#define MINA  'M'
#define SUB  'S'
#define JOG  'U'
#define CC  'I'

//Função que inicializa posição do jogador.
void posjog(char **mesa,int lin, int col){
    srand(time(NULL));
    int i = rand() % 2;
    int j = rand() % 2;
    if(i==1){
        i = lin;
    }
    if(j==1){
        j = col;
    }
    mesa[i][j] = JOG;
}

//Função que inicializa a matriz vazia.
void arfill(char **mesa,int lin, int col){
    for(int i=0;i<=lin;i++){
        for(int j=0;j<=col;j++){
            mesa[i][j]='.';
        }
    }
}

void show(char **mesa,int lin,int col){
    for(int i=0;i<=lin;i++){
            for(int j=0;j<=col;j++){
                printf("%c",mesa[i][j]);
            }
            printf("\n");
        }
}

int main(void) {
    char campo[9][9]; //matriz de jogo
    arfill(campo,9,9);
    posjog(campo,9,9);
    show(campo,9,9);
    return EXIT_SUCCESS;
}

我的代码总是崩溃,我不知道为什么。 你们中的一些人可以帮我解决这个问题吗? 我在 Eclipse 上也有一些警告... “从不兼容的指针类型传递 'arfill' 的参数 1 [默认启用]” "预期为 'char **' 但参数类型为 'char (*)[9]'"

【问题讨论】:

  • 您是否使用调试器检查过它崩溃的行。还有,什么样的问题?它不编译/构建吗?如果它构建,使用调试器找出它开始出现问题的行。
  • void func(int lin, int col, char mesa[lin][col])i&lt;=lin,j&lt;=col --> i&lt;lin,j&lt;col
  • 数组不是指针,这个警告告诉你一切。

标签: c arrays function pointers char


【解决方案1】:

数组索引从 0 开始,以数组大小 - 1 结束。

所以,这个

for(int i=0;i<=lin;i++){
    for(int j=0;j<=col;j++){

对于大小为 9x9 的数组,将从 0 变为 9,因此它将超出范围。

类似的其余代码也有类似的问题。


您绝对应该启用编译器的警告(-Wall 标志就足够了)。警告最好被我们当作错误对待。这是我得到的:

../main.c: In function ‘main’:
../main.c:45:5: warning: passing argument 1 of ‘arfill’ from incompatible pointer type [enabled by default]
../main.c:26:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
../main.c:46:5: warning: passing argument 1 of ‘posjog’ from incompatible pointer type [enabled by default]
../main.c:12:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
../main.c:47:5: warning: passing argument 1 of ‘show’ from incompatible pointer type [enabled by default]
../main.c:34:6: note: expected ‘char **’ but argument is of type ‘char (*)[9]’
Finished building: ../main.c

因此,解决此问题的一种方法是使用define 来确定矩阵的大小。那么你的代码应该是这样的:

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

//definições de constantes usadas para agilizar a implementação.
#define MINA  'M'
#define SUB  'S'
#define JOG  'U'
#define CC  'I'

#define N 9
#define M 9

//Função que inicializa posição do jogador.
void posjog(char mesa[N][M]) {
  srand(time(NULL));
  int i = rand() % 2;
  int j = rand() % 2;
  if (i == 1) {
    i = N;
  }
  if (j == 1) {
    j = M;
  }
  // if i and j have the value of N and M, this will go out of bounds
  // mesa[i][j] = JOG;   <---- modify this
}

//Função que inicializa a matriz vazia.
void arfill(char mesa[N][M]) {
  // Replaces <= with < in both for loops
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      mesa[i][j] = '.';
    }
  }
}

void show(char mesa[N][M]) {
  // Replaces <= with < in both for loops
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      printf("%c", mesa[i][j]);
    }
    printf("\n");
  }
}

int main(void) {
  char campo[N][M];  //matriz de jogo
  arfill(campo);
  posjog(campo);
  show(campo);
  return EXIT_SUCCESS;
}

当然,您可以为数组动态分配内存,这将摆脱define。在这种情况下,不要忘记取消分配内存。

【讨论】:

    猜你喜欢
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多