通过引用的2D数组

时间:2014-11-15 15:42:37

标签: c arrays function pointers char

我有这个......

#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]'”

1 个答案:

答案 0 :(得分: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。在这种情况下,不要忘记取消分配你的记忆。