0

Below is my MRE. Essentially, I am making a board game, and I need to do two things. Make the board, and after each turn, print it. Since the board changes after each turn, I have them in two separate functions makeBoard and printBoard below, I just put them in the MRE. The board also gets its dimensions from args and so it can be any size that the player chooses. But when I print out the empty board, it says the error mentioned above.

for context the matrix is supposed to be a 5 by 5 then it should print like this:

. . . . . . . . . . . . . . . . . . . . . . . . .

edit: I can not test if the matrix prints with the correct formatting since I am getting this error, so if there are issues with that part of the code, I will take care of them later.

int main()
{
    int x = 5;
    int y = 5;
    
    char board[5][5];
     
     // this is where I am filling the board with its 'null' values
     for(int i =0; i<x; i++){
        for(int j =0; j<y; j++){
            // this is where the error takes place
            board[i][j]= ".";
        }
    }
    // this is where I am printing the board, I have a specific format so, 
    // that there is a space between each dot
    for(int i=0; i<x; i++){
        for(int j = 0; j<y; j++){
            if(j<y-1){
                printf("%c",board[i][j]);
                printf(" ");
            }
            else if(j=y-1){
                printf("%c", board[i][j]);
            }
            printf("\n");
            
        }
    }
}
  • Unless you are deliberately trying to make your life difficult, you might consider better names than `x`, `y`, and `i` & `j`. The usual _convention_ is that `x` is horizontal and `y` is vertical. Your print loop seems to be iterating _vertically_ **inside** a _horizontal_ iteration. That's not how it will appear on the screen.. (And, as presented, there's a LF between each dot, too...) – Fe2O3 Oct 02 '22 at 08:20

1 Answers1

0

Instead of the line

board[i][j]=".";

you should have written:

board[i][j]='.';

Double quotes(") are used for string literals, and single quotes(') are used for characters.

You get this warning because strings are stored as pointers to the first character(char*). The characters inside of strings are stored in memory addressed from the value of the pointer used to store strings to the first null character '\0' after the first one in the string(in memory).

Also, a warning isn't an error. Errors prevent code from getting compiled, while warnings notify of potential problems with the code.

Note: Use unsigned int instead of int for indexes because int is signed by default.

markoj
  • 150
  • 10