gpt4 book ai didi

c - 如何检查一个字符是句点 ('.' ) 在 c

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:37 25 4
gpt4 key购买 nike

所以我的程序必须接收读取字符网格的 txt 文件。每个字符代表一个特定的命令。句点(句号)表示继续执行上一条命令。

但是当我编译我的代码时 gcc -Wall -pedantic thebox.c -o thebox,我收到了这个警告:

suggest parentheses around assignment used as truth value

这是警告所指函数的第一部分。警告显示第 78 行,即 if(command = ('.') 行 -

int* getCommand(int next[2],char** gridArray)
{
/* a function to return the command on the next position */
int nextX = next[0];
int nextY = next[1];
char newCommand;
char command = gridArray[nextX][nextY];
if(command = ('.')) {
newCommand = '1';
gridArray[nextX][nextY] = newCommand;
}

如何修复此警告?

最佳答案

您使用 == 检查是否相等。

您可以用来帮助避免将来出现类似问题的技术是扭转您的比较:

        if('.' == command) {

如果忘记了额外的=,编译器会报错。此技术并不总是适用(例如当您比较两个变量时),但它适用的频率足够高,因此值得付出努力。

关于c - 如何检查一个字符是句点 ('.' ) 在 c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com