Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
预览二维阵列8x8和x27;我不能在c工作_C_Multidimensional Array - Fatal编程技术网

预览二维阵列8x8和x27;我不能在c工作

预览二维阵列8x8和x27;我不能在c工作,c,multidimensional-array,C,Multidimensional Array,我正在尝试制作一个显示棋盘/棋盘的代码。我给了棋盘上的每个方块一个数字,所以我可以手动选择棋子的位置,作为未来编程的目的。这就是我得到的: #include <stdio.h> //{",",",",",",","}, void main() { int board[8][8] = { {'1','2','3','4','5','6','7','8'}, {'9','10','11','12','13','14','15','16'}, {'17','18','19','20','2

我正在尝试制作一个显示棋盘/棋盘的代码。我给了棋盘上的每个方块一个数字,所以我可以手动选择棋子的位置,作为未来编程的目的。这就是我得到的:

#include <stdio.h>
//{",",",",",",","},
void main()
{
int board[8][8] = {
{'1','2','3','4','5','6','7','8'},
{'9','10','11','12','13','14','15','16'},
{'17','18','19','20','21','22','23','24'},
{'25','26','27','28','29','30','31','32'},
{'33','34','35','36','37','38','39','40'},
{'41','42','43','44','45','46','47','48'},
{'49','50','51','52','53','54','55','56'},
{'57','58','59','60','61','62','63','64'}
};

      /* Display the checkerboard */
printf("\n\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]);

}
警告如下:

main.c:13:37: warning: multi-character character constant[-Wmultichar]                                                                        
{'57','58','59','60','61','62','63','64'}  

看起来一个角色不能打印高于9的字符?!我想要的是一个简单的棋盘预览(带有2d数组)。我试图更改代码中的某些部分,但没有任何运气。。。有什么想法吗?

替换你的电路板定义
'xx'
=>
xx

int board[8][8] = {
{1,2,3,4,5,6,7,8},
    ....
{57,58,59,60,61,62,63,64}
}; 
更改
printf
格式(
%c
=>
%2d

注:

  • 当你写
    intfoo='0'时,则要求编译器将字符“0”的值存储在变量
    foo

    字符
    '0'
    的值与
    0
    不同

  • 当你写
    intfoo='10',编译器告诉您它不理解您的意思,因为如果
    '0'
    '1'
    是字符,
    '10'
    没有任何意义

  • %c
    %d
    printf
    中不同:

    • printf(“%c”,foo)
      将要求打印代码为
      foo

    • printf(“%d”,foo)
      将要求打印
      foo的值

    • printf(“%c”,“a”)
      将要求打印字符
      a
    • printf(“%d”,“a”)
      将要求打印字符
      a的值

更换电路板定义
'xx'
=>
xx

int board[8][8] = {
{1,2,3,4,5,6,7,8},
    ....
{57,58,59,60,61,62,63,64}
}; 
更改
printf
格式(
%c
=>
%2d

注:

  • 当你写
    intfoo='0'时,则要求编译器将字符“0”的值存储在变量
    foo

    字符
    '0'
    的值与
    0
    不同

  • 当你写
    intfoo='10',编译器告诉您它不理解您的意思,因为如果
    '0'
    '1'
    是字符,
    '10'
    没有任何意义

  • %c
    %d
    printf
    中不同:

    • printf(“%c”,foo)
      将要求打印代码为
      foo

    • printf(“%d”,foo)
      将要求打印
      foo的值

    • printf(“%c”,“a”)
      将要求打印字符
      a
    • printf(“%d”,“a”)
      将要求打印字符
      a的值

不能将字符串放在单引号字符中。必须是唯一的字符。 但硬编码字符串中的连续整数列表没有意义,因此:

带有公式的简单双循环更紧凑,并节省大量复制/粘贴:

#include <stdio.h>
void main()
{
int i,j;

      /* Display the checkerboard */
     printf("\n\n");
     for (i=0;i<8;i++)
     {
       for (j=0;j<8;j++)
       {
         printf("%3d ",i*8+(j+1));
       }
       printf("\n---+---+---+---+---+---+---+---\n");

   }
  }
增强版,具有更好的框架(只是为了好玩)


仍然不是ASCII艺术大师,但更好:)

不能将字符串放在单引号字符中。必须是唯一的字符。 但硬编码字符串中的连续整数列表没有意义,因此:

带有公式的简单双循环更紧凑,并节省大量复制/粘贴:

#include <stdio.h>
void main()
{
int i,j;

      /* Display the checkerboard */
     printf("\n\n");
     for (i=0;i<8;i++)
     {
       for (j=0;j<8;j++)
       {
         printf("%3d ",i*8+(j+1));
       }
       printf("\n---+---+---+---+---+---+---+---\n");

   }
  }
增强版,具有更好的框架(只是为了好玩)


仍然不是ASCII艺术大师,但更好:)

您需要双引号字符串而不是字符,以及循环中的%2s格式。很严重,您不能像这样复制/粘贴printf的行。请删除所有单引号,并在
printf
int
不是
char
中使用
%d“
。多符号字符文字没有任何意义。因此编译器发出警告。查找表中只包含相邻的数字也没有任何意义,您可以在运行时计算这些数字。您需要双引号字符串而不是字符,并且在循环中使用%2s格式。很严重,您不能像这样复制/粘贴printf的行。请删除所有单引号,并在
printf
int
不是
char
中使用
%d“
。多符号字符文字没有任何意义。因此编译器发出警告。拥有一个只包含相邻数字的查找表也没有任何意义,您可以在运行时计算这些数字。但为什么您希望拥有一个只包含常量、相邻数字的查找表呢?这些数字似乎永远不会改变,因为它们的目的是枚举平方。谢谢,这些最后的注释真的很有用。不过,为什么你会想要一个只有常量、相邻数字的查找表呢?这些数字似乎永远不会改变,因为它们的目的是枚举平方。谢谢,最后的注释真的很有用。你是对的,它使代码更加紧凑!你的艺术技巧似乎很好;PYea你是对的,它使代码更加紧凑!你的艺术技巧似乎很好;P
  1   2   3   4   5   6   7   8
---+---+---+---+---+---+---+---
  9  10  11  12  13  14  15  16
---+---+---+---+---+---+---+---
 17  18  19  20  21  22  23  24
---+---+---+---+---+---+---+---
 25  26  27  28  29  30  31  32
---+---+---+---+---+---+---+---
 33  34  35  36  37  38  39  40
---+---+---+---+---+---+---+---
 41  42  43  44  45  46  47  48
---+---+---+---+---+---+---+---
 49  50  51  52  53  54  55  56
---+---+---+---+---+---+---+---
 57  58  59  60  61  62  63  64
---+---+---+---+---+---+---+---
#include <stdio.h>
void main()
{
int i,j;

   /* Display the checkerboard */
printf("\n\n\n|---+---+---+---+---+---+---+---+\n");

for (i=0;i<8;i++)
{
   printf("|");
   for (j=0;j<8;j++)
   {
      printf("%2d |",i*8+(j+1));
   }
   printf("\n|---+---+---+---+---+---+---+---+\n");

}
|---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---+---+---+---+---+---+---+---+
| 9 |10 |11 |12 |13 |14 |15 |16 |
|---+---+---+---+---+---+---+---+
|17 |18 |19 |20 |21 |22 |23 |24 |
|---+---+---+---+---+---+---+---+
|25 |26 |27 |28 |29 |30 |31 |32 |
|---+---+---+---+---+---+---+---+
|33 |34 |35 |36 |37 |38 |39 |40 |
|---+---+---+---+---+---+---+---+
|41 |42 |43 |44 |45 |46 |47 |48 |
|---+---+---+---+---+---+---+---+
|49 |50 |51 |52 |53 |54 |55 |56 |
|---+---+---+---+---+---+---+---+
|57 |58 |59 |60 |61 |62 |63 |64 |
|---+---+---+---+---+---+---+---+