-1

This is a fragment of a ticktacktoe game that I started. When I compile it, the Wint conversion warning pops up, saying that clear_table(board); and display_table(board); are making integers from pointer without a cast. I'm really new at C and I'm not sure how to fix this. Plus I'm not allowed to change the main function at all.

#include <stdio.h>
#define SIZE 3

void display_table(char board);
void clear_table(char board);

int main()
{
    char board[SIZE][SIZE];
    int row, col;

    clear_table(board);

    display_table(board);

    return 0;
}

void clear_table(char board) {
    int row = 0, col = 0;
    for(row = 0; row < SIZE; row++) {
        for(col = 0; col < SIZE; col++) {
            board = '_';
        }
    }

    return;
}

void display_table(char board) {
    printf("The current state of the game is: \n");
    int row = 0, col = 0;
    for(row = 0; row < SIZE; row++) {
        for(col = 0; col < SIZE; col++) {
            printf("%c ", board);
        }
        printf("\n");
    }

    return;
}

Please help?

4
  • Hint: char board can hold only one single char value. Yet, you feed those routines with a char *.
    – Jongware
    Mar 22, 2018 at 21:23
  • 3
    @usr2564301 nope, 2D array, that's different. Mar 22, 2018 at 21:23
  • board is a 2D char array, not a char Mar 22, 2018 at 21:24
  • 1
    :) It's still a world away from a single char though.
    – Jongware
    Mar 22, 2018 at 21:25

1 Answer 1

1

Pass the board char board[SIZE][SIZE] as an array to your functions and inside the function access elements by array subscript operators: [][]:

#include <stdio.h>
#define SIZE 3

void display_table(char  board[][SIZE]);
void clear_table(char  board[][SIZE]);

int main(void)
{
    char board[SIZE][SIZE];
    int row, col;

    clear_table(board);

    display_table(board);

    return 0;
}

void clear_table(char  board[][SIZE]) {
    int row = 0, col = 0;   
    for(row = 0; row < SIZE; row++) {
        for(col = 0; col < SIZE; col++) {
            board[row][col]= '_';
        }   
    } 
    return;
}

void display_table(char board[][SIZE]) {
    printf("The current state of the game is: \n");
    int row = 0, col = 0;
    for(row = 0; row < SIZE; row++) {
        for(col = 0; col < SIZE; col++) {
            printf("%c ", board[row][col]);           
        }
        printf("\n");   
    }
    return;
}

Output:

The current state of the game is:                                                                                                            
_ _ _                                                                                                                                        
_ _ _                                                                                                                                        
_ _ _                                                                                                                                        
4
  • clear_table can be replaced by a good old' memset(board,'_',SIZE*SIZE) Mar 22, 2018 at 21:37
  • Thank you so much! It makes more sense now! Mar 22, 2018 at 21:39
  • @Jean-FrançoisFabre Thank you kindly for your comment! That is very true and it is good optimization for the program! I did not want to do so in order to correct board= '_'; bug. Thank you again for pointing that out!
    – sg7
    Mar 22, 2018 at 21:42
  • OP assignment is probably not going that far, yes. Mar 22, 2018 at 21:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.