当我用 python 导师可视化我的代码时,数组的值变成空字符串,当 if 条件进动时就会发生这种情况,我不明白为什么会发生这种情况。有人可以解释为什么会发生这种情况并提供解决方案吗?
#include <stdio.h>
int countWords(char *src, char toFind)
{
int numWords;
int i;
int d;
numWords = 0;
i = 0;
d = 0;
while (*src)
{
if (*src =! (toFind)) //it happens here*?*
numwords++;
src++;
}
}
int main()
{
char arr[] = "abc,ffg,ijk";
char c = ',';
int res = 0;
char *ptr;
ptr = arr;
res = countWords(ptr,c);
printf("%d", res);
return 0;
}
最佳答案
你犯了一个拼写错误,如果你想进行比较,你应该使用 !=
而不是 =!
这是两个运算符,告诉编译器评估变量并将相反的结果赋给*src
。
此外,假设您想要字数而不是字符数,您可能使用了错误的运算符。使用 ==
可以解决这个问题。
关于c - 值变成空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356855/