char赋值给* char无效

时间:2016-07-16 22:25:15

标签: c pointers char variable-assignment

这是我主要使用的功能。问题在于char任务。

void takeTurn(int *iap, int *tile, char *cap) {
    //*iap is 1 right now

    printf("\nThe current active player is %d. His character is %c", *iap, *cap);
    //prints The current active player is 1. His character is q.

    if (*iap == 1) *cap == 'X';
    if (*iap == 2) *cap == 'O';

    printf("\nThe current active player is %d. His character is %c", *iap, *cap);.
    //prints The current active player is 1. His character is q.

    . . . 
}

需要做什么才能*cap将正确的char分配给正确的有效玩家?

1 个答案:

答案 0 :(得分:2)

您正在使用=等式比较运算符,而您应该使用*cap == 'X'; *cap == 'O'; 赋值运算符。

更改这些陈述:

*cap = 'X';
*cap = 'O';

改为:

///test.c
#include <stdio.h>

int main(){
  double d = 13.37;
  char buf[128];
  snprintf(buf, 128, "%f", d);

  return 0;
}