0

我正在尝试使用函数打印二维数组,但我不断收到错误消息"pointer expected"

我正在尝试制作一个战舰类型的网格。我可以打印出坐标行和列,但我实际上根本无法打印二维数组(每个元素中都包含“.”)。

任何帮助将不胜感激,我对此很陌生。谢谢!:)

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

int length;
int width;
int i;
int j;
char invisible_board;


void board_setup(int *rows, int *columns){
    char *invisible_board[*rows][*columns];
char *player_board[*rows][*columns];

for (i = 0; i < *rows; i++){
    for (j = 0; j < *columns; j++){
        invisible_board[i][j] = ".";    //Sets all elements in hidden board to water
    }       
}

for (i = 0; i < *rows; i++){
    for (j = 0; j < *columns; j++){
        player_board[i][j] = ".";
    }
}
}

void display(int *rows, int *columns, char *invisible_board){

printf("   ");
for (i=1; i < *rows +1;i++){
    printf("%d  ",i);
}
printf("\n");                       //Prints top row of co-ordinates

for (i=1; i < *columns+1;i++){
    printf("%d ",i);
    for (j=0;j < *columns;j++){         //Prints left column of co-    ordinates and rows of game board
            printf(" %c ",invisible_board[i-1][j]);
        }
        printf("\n");
        }

}

int main(void){

    printf("Please enter the amount of rows in your board\n");
    scanf("%d",&length);
    printf("Please enter the amount of columns in your board\n");
    scanf("%d",&width);

    board_setup(&length,&width);
    display(&length,&width,&invisible_board);

    return (0);
}
4

1 回答 1

1

这是我可以对您的代码进行的最简单的更改,以让您使用工作代码....现在....这还不是好的代码。但让你开始。

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

int length;
int width;
int i;
int j;
char invisible_board[100][100];   // dynamically allocate....
char player_board[100][100];   // dynamically allocate....


void board_setup(int *rows, int *columns){  
    for (i = 0; i < *rows; i++){
        for (j = 0; j < *columns; j++){
            invisible_board[i][j] = '.';    //Sets all elements in hidden board to water
        }       
    }

    for (i = 0; i < *rows; i++){
        for (j = 0; j < *columns; j++){
            player_board[i][j] = '.';
        }
    }
}

void display(int *rows, int *columns){

    printf("   ");
    for (i=1; i < *rows +1;i++){
        printf("%d  ",i);
    }
    printf("\n");                       //Prints top row of co-ordinates

    for (i=1; i < *columns+1;i++){
        printf("%d ",i);
        for (j=0;j < *columns;j++){         //Prints left column of co-    ordinates and rows of game board
            printf(" %c ",invisible_board[i-1][j]);
        }
        printf("\n");
    }

}

int main(void){

    printf("Please enter the amount of rows in your board\n");
    scanf("%d",&length);
    printf("Please enter the amount of columns in your board\n");
    scanf("%d",&width);

    board_setup(&length,&width);
    display(&length,&width);

    return (0);
}
于 2013-01-29T01:05:46.563 回答