函数里怎么传递二元数组参数

wangdong20 2011-08-08 04:29:17
写了一个展示棋盘的函数
void display(board[][SIZE]) //编译时这里有错误,不知道该怎么改
{
int row=0;
int col=0;
char col_label='a';

printf("\n");
for(col=0;col<SIZE;col++)
printf(" %c",col_lable+col);
for(row=0;row<SIZE;row++)
{
printf("\n");
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n%d|",row+1);
for(col=0;col<SIZE;col++)
printf(" %c |",board[row][col]);
}
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n");
}
就在函数参数那一行总是编译错误
...全文
344 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2011-08-09
  • 打赏
  • 举报
回复
void display(board[][SIZE]) //编译时这里有错误,不知道该怎么改

错误是没有数据类型,二维数组 board[][SIZE] 传递 ok
wangdong20 2011-08-09
  • 打赏
  • 举报
回复
呵呵 谢谢大家了
_了凡_ 2011-08-08
  • 打赏
  • 举报
回复
参考一下:

#include <stdio.h>

#define SIZE 6

//Function prototypes
void display(char (*board)[SIZE]);

int main(int argc, char **argv)
{
char board[SIZE][SIZE];
int row=0;
int col=0;
char again=0;

printf("\nREVERSI\n\n");
printf("You can go first on the first game,then we will take turns.\n");
printf(" You will be white-(O)\n Iwill be black-(@).\n");
printf("Select a square for your move by typing a digit for the row\n "
"and a letter for the column with no space between.\n");
printf("\nGood luck! Press Enter to start.\n");
scanf("%c",&again);

//Blank all the board squares
for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
board[row][col]=' ';

//Place the initial four counters in the center
board[SIZE/2-1][SIZE/2-1]=board[SIZE/2][SIZE/2]='O';
board[SIZE/2-1][SIZE/2]=board[SIZE/2][SIZE/2-1]='@';

display(board);
return 0;
}

//The function to display the board
void display(char (*board)[SIZE])
{
int row=0;
int col=0;
char col_label='a';

printf("\n");
for(col=0;col<SIZE;col++)
printf(" %c",col_label+col);
for(row=0;row<SIZE;row++)
{
printf("\n");
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n%d|",row+1);
for(col=0;col<SIZE;col++)
printf(" %c |",board[row][col]);
}
printf("\n +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n");
}

运行结果:
REVERSI

You can go first on the first game,then we will take turns.
You will be white-(O)
Iwill be black-(@).
Select a square for your move by typing a digit for the row
and a letter for the column with no space between.

Good luck! Press Enter to start.


a b c d e f
+---+---+---+---+---+---+
1| | | | | | |
+---+---+---+---+---+---+
2| | | | | | |
+---+---+---+---+---+---+
3| | | O | @ | | |
+---+---+---+---+---+---+
4| | | @ | O | | |
+---+---+---+---+---+---+
5| | | | | | |
+---+---+---+---+---+---+
6| | | | | | |
+---+---+---+---+---+---+

PS:我修正了你程序中的一处逻辑问题,否则图形最下面一行有问题。

正好有空,帮你分析一下你的这个程序:
1、“按回车键开始”这个功能不要通过scanf来做,用其他的类似getch之类的函数吧,否则你会发现
后面若还有输入的地方时,老是会莫名多出一个回车来,或是其他输入问题。
2、注意main函数的定义。
3、风格问题……


预祝你的这个游戏顺利完成……
东莞某某某 2011-08-08
  • 打赏
  • 举报
回复
void display(char board[][SIZE])//err1 无数据类型
{
int row=0;
int col=0;
char col_label='a';//err2 变量标识符不一致

printf("\n");
for(col=0;col<SIZE;col++)
printf(" %c",col_lable+col);
wangdong20 2011-08-08
  • 打赏
  • 举报
回复
我把完整的代码贴出来
各位大侠看看该怎么改
//Program 9.9 REVERSI An Othello-type game
#include <stdio.h>

#define SIZE 6

//Function prototypes
void display(char board[][SIZE]);

void main()
{
char board[SIZE][SIZE]={ 'O' };
int row=0;
int col=0;
char again=0;

printf("\nREVERSI\n\n");
printf("You can go first on the first game,then we will take turns.\n");
printf(" You will be white-(O)\n Iwill be black-(@).\n");
printf("Select a square for your move by typing a digit for the row\n "
"and a letter for the column with no space between.\n");
printf("\nGood luck! Press Enter to start.\n");
scanf("%c",&again);

//Blank all the board squares
for(row=0;row<SIZE;row++)
for(col=0;col<SIZE;col++)
board[row][col]=' ';

//Place the initial four counters in the center
board[SIZE/2-1][SIZE/2-1]=board[SIZE/2][SIZE/2]='O';
board[SIZE/2-1][SIZE/2]=board[SIZE/2][SIZE/2-1]='@';

display(board);

}
//The function to display the board
void display(board[][SIZE])
{
int row=0;
int col=0;
char col_label='a';

printf("\n");
for(col=0;col<SIZE;col++)
printf(" %c",col_lable+col);
for(row=0;row<SIZE;row++)
{
printf("\n");
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n%d|",row+1);
for(col=0;col<SIZE;col++)
printf(" %c |",board[row][col]);
}
printf(" +");
for(col=0;col<SIZE;col++)
printf("---+");
printf("\n");
}
wangdong20 2011-08-08
  • 打赏
  • 举报
回复
是char型
视觉 2011-08-08
  • 打赏
  • 举报
回复
二维数组的类型呢?
qianfoyuan 2011-08-08
  • 打赏
  • 举报
回复
void display(board[][SIZE]) //编译时这里有错误,不知道该怎么改

改为: void display(int board[][SIZE],int nCount);
mimi122 2011-08-08
  • 打赏
  • 举报
回复
board[row*SIZE+col])表示二维数组
wangdong20 2011-08-08
  • 打赏
  • 举报
回复
我的这个是二元数组
补充一下
之前的SIZE
是#define SIZE 6
mimi122 2011-08-08
  • 打赏
  • 举报
回复
数组应该以指针形式作为函数的形参
void display(*board)
调用方法
printf(" %c |",board[row*SIZE+col]);

69,393

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧