简体   繁体   English

char数组初始化程序中的多余元素

[英]excess elements in char array initializer

I'm trying to improve my lousy C skills so I'm writing a tic tac toe program. 我正在尝试提高糟糕的C技能,所以我正在编写井字游戏程序。 It's only in its first steps but I would like to be able to print a board before going into the whole algorithms of the game itself. 这只是第一步,但我希望能够在进入游戏本身的整个算法之前先印制电路板。 I'm planning to use 1 for x and 0 for circle in order to be able to use sums later. 我计划对x使用1,对圆使用0,以便以后可以使用总和。 I'm working with gcc (ubuntu) and am getting this error: 我正在使用gcc(ubuntu),并且遇到此错误:

xo.c:11:3: error: (near initialization for ‘board[0]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[0]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[1]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[1]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[2]’)
xo.c:11:3: error: excess elements in char array initializer
xo.c:11:3: error: (near initialization for ‘board[2]’)

I would like to initiate a 2 dimensional array of 3x3 for the board. 我想为该板启动一个3x3的二维数组。 I don't understand why is there an excess, I initialized a [3][3] and entered 9 elements. 我不明白为什么会有多余的内容,因此我初始化了一个[3] [3]并输入了9个元素。 Here is the code I've written: 这是我编写的代码:

#include<stdio.h>

/***** Create two dimensional array full of blank spaces
    1 marks the X , 0 marks the circle.
    Let use enter coordinates (line and coloumn) for placing the 1 (x) and automatically print the board with the computers' move.
*****/ 
int i = 0;
int j = 0;
void main() {
    char board[3][3] = {
        {" " , " " , " "} , {" " , " " , " "} , {" " , " " , "t"}
    };
    for(i=0 ; i<3 ; i++) {
        for(j=0 ; j<3 ; j++) {
            printf("%c " , board[i][j]);
        }
        printf("\n");
    }
}

" " is a string ( char * ) literal. " "是字符串( char * )文字。 You want ' ' for a single char . 你想' '单个char

Change: 更改:

    char board[3][3] = {
        {" " , " " , " "} , {" " , " " , " "} , {" " , " " , "t"}
    };

to: 至:

    char board[3][3] = {
        {' ' , ' ' , ' '} , {' ' , ' ' , ' '} , {' ' , ' ' , 't'}
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 char数组初始值设定项错误中的多余元素 - Excess elements in char array initializer error 错误:char数组初始化程序中的多余元素 - error: excess elements in char array initializer 数组初始化程序中的多余元素 - Excess elements in array initializer 警告:数组初始值设定项中的多余元素 - warning: excess elements in array initializer 遇到错误:打印出字符串数组期间 char 数组初始值设定项中的元素过多 - Faced an error: excess elements in char array initializer during print out of array of strings 通过指向 2D 字符数组的指针访问。 警告:数组初始值设定项中的多余元素 - access via pointer to 2D char array. warning: excess elements in array initializer C:警告:数组初始值设定项中的元素过多; &#39;xxx&#39; 即将初始化; 需要“char *”,但类型为“int” - C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’ 编译警告。数组初始化程序中的多余元素 - Compilation Warning.Excess elements in array initializer 在数组初始值设定项中有多余的元素可以吗? - Is it ok to have excess elements in array initializer? 指向整数数组的指针的标量初始值设定项的多余元素 - Excess elements of scalar initializer for pointer to array of ints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM