1

I'm trying to improve my lousy C skills so I'm writing a tic tac toe program. 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. I'm working with gcc (ubuntu) and am getting this error:

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. I don't understand why is there an excess, I initialized a [3][3] and entered 9 elements. 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");
    }
}
0

1 Answer 1

4

" " is a string (char *) literal. You want ' ' for a single char.

Change:

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

to:

    char board[3][3] = {
        {' ' , ' ' , ' '} , {' ' , ' ' , ' '} , {' ' , ' ' , 't'}
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.