kabeersvohra
kabeersvohra

Reputation: 1059

Type mismatch in C

I wrote this code but it is saying that there is a type conflict on the declaration of display board. It looks fine to me. Can someone help please?

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int board[9][9] = {
            {0,0,5,9,0,2,3,8,7},
            {9,4,0,1,0,7,6,0,2},
            {2,8,7,5,3,0,4,0,0},
            {0,5,2,3,6,0,0,0,1},
            {4,0,9,0,5,1,2,6,8},
            {1,0,6,0,2,0,0,3,4},
            {5,0,8,4,0,0,1,9,6},
            {3,9,0,0,1,8,7,2,0},
            {0,6,0,2,9,5,8,0,3}
    };
    char* board_output = (display_board(board));
    printf ("%s", &board_output);
    return 0;
    }

    char* display_board (int board_input[9][9])
    {
        int i;
        int j;
        char* output = "";
        for (i=0; i<9; i++) {
            for (j=0; j<9; j++) {
                    output = strcat(strcat(output, board_input[i][j]),  ", ");
            }
            output = strcat(output, ", ");
        }

        return output;
    }

Thanks

Kabeer

Upvotes: 0

Views: 383

Answers (2)

dev
dev

Reputation: 137

Prototype of function is needed before calling it in main()

Add this

char* display_board (int board_input[9][9]);

just after headers

Upvotes: 1

2501
2501

Reputation: 25753

You need a function declaration before calling that function:

char* display_board (int board_input[9][9]) ;  

You also try to write into a string literal output here:

strcat(strcat(output, board_input[i][j]),  ", "); //this syntax is not readable
                                                  //split the calls into
                                                  //separate lines

Instead reserve some memory for it

char* output = calloc( 512 , sizeof( char ) ) ;

And strcat() requires a string not an integer, so this is not correct: strcat(output, board_input[i][j])


There may be more errors, first fix those and then try again.

Upvotes: 4

Related Questions