【问题标题】:why am I getting this format error ‘%c’ expects type ‘int’为什么我收到此格式错误“%c”需要类型“int”
【发布时间】:2013-04-20 06:08:04
【问题描述】:

为什么我的函数会出现此错误或类型冲突? 其中 player 只是我只想输出的一个角色,以显示下一个玩家将去哪里

错误

tictac.c:94: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’
tictac.c:94: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’

代码

void move(char player)
{
        int place;
        printf("player %c, enter placement: \n", &player);
        scanf("%d", &place);

        if (place == 1)
                board[0][0] = player;
        else if (place == 2)
                board[0][1] = player;
        else if (place == 3)
                board[0][2] = player;

        else if (place == 4)
                board[1][0] = player;
        else if (place == 5)
                board[1][1] = player;
        else if (place == 6)
                board[1][2] = player;

        else if (place == 7)
                board[2][0] = player;
        else if (place == 8)
                board[2][1] = player;
        else if (place == 9)
                board[2][2] = player;
}

【问题讨论】:

  • 因为您将 address 传递给 character 格式说明符?也许???诚然,许多 C 编译器错误有点神秘,但很难说得更清楚。它实际上告诉你格式说明符是什么,它期望什么,以及你实际上传递了什么。没有自动为您修复它,这是您所希望的最好的。
  • 为什么会收到一个错误,告诉您为什么会收到错误?
  • 使用board[(place-1) / 3][(place-1) % 3] = player;而不是大量的ifelse if可以提高速度和代码美观。

标签: c syntax typeerror


【解决方案1】:

你不应该在printf中使用&player

改变

printf("player %c, enter placement: \n", &player);

printf("player %c, enter placement: \n", player);

【讨论】:

  • 我以为playerint,但后来我意识到并纠正了。
【解决方案2】:

简单试试

 printf("player %c, enter placement: \n", player);

没有任何& 运算符的地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多