0
votes

This c code

int rhdDEboard2(){

    int board[64]={0};
    int rc;
    printf("\n");
    for (rc=0;rc<8;rc++)
        printf("\t %i %i %i %i %i %i %i %i\n",board[rc]);
    printf("\n");
    return 0;
}

compiled with MinGW in Win8 PC gives this error: : warning: format '%i' expects a matching 'int' argument [-Wformat]

I do not understand why! The board array is declared as type int..isn't it? (Does C treat the array as a pointer type?). I have tried using %p as format specifier but that doesn't work and nor does using %d. I understand what the warning says, but not why it says it, nor how to fix it, and feel I must be missing something quite basic, simple and straightforward here, but I do not understand this compiler warning.I'm grateful for help to try and get me to understand this C compiler warning and how I should fix it..many thanks

1
You have 8 %i in the formatting string, but you only have 1 board[rc] in the rest of the arguments. It's complaining about the other 7 that don't have a corresponding argument.Barmar
Why have you declared an array with 64 elements, but you're only printing the first 8?Barmar
Hi Barmar..thanks for your helpful reply...On editing to:printf("\t %i %i %i %i %i %i %i %i\n",board[rc],board[rc],board[rc],board[rc],board[rc],board[rc],board[rc],board[rc]); I now get the 8x8 board output I wanted...I am repeatedly printing 8 elements on 8 new lines to form an 8x8 outputted board structure/display. Should I be doing this in a better way?rpd
@rpd they way you fixed it will it now would print the same value 8 times. Use board[rc * 8]. board[rc * 8 + 1], .., board[rc * 8 + 7]).ryanpattison

1 Answers

0
votes

As commenters mentioned, you should try any of the way.

int rhdDEboard2() {

    int board[64]={0};
    int i,j;
    printf("\n");
    for (i=0;i<8;i++) {
        printf("\t");
        for(j=0;j<8;j++)
            printf(" %i",board[(i*8)+j]);
        printf("\n");
    }
    printf("\n");
    return 0;
}

or

int rhdDEboard2() {

    int board[64]={0};
    int rc;
    printf("\n");
    for (rc=0; rc<64; rc+=8)
        printf("\t %i %i %i %i %i %i %i %i\n",
                board[rc]  ,board[rc+1],board[rc+2],board[rc+3],
                board[rc+4],board[rc+5],board[rc+6],board[rc+7] );
    printf("\n");
    return 0;
}