gpt4 book ai didi

c - 警告 : Comparison between pointer and integer and escape characters

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

1) 我在 https://www.tutorialspoint.com/compile_c_online.php 上测试了我的代码它以某种方式通过显示 1,3,27,23,19 的理想输出来工作,但也给了我错误消息,这很奇怪,因为我的所有函数都没有任何整数。谁能告诉我为什么显示错误的行是错误的?

2)我怀疑这与字符'没有被正确处理有关,但我不知道如何打印出除%%之外的所有特殊字符> 和 \\。有人可以告诉我他们的完整列表吗?

编辑:我意识到实际上我弄错了 2 个转义字符(在 https://www.geeksforgeeks.org/escape-sequences-c/ 上找到了它的正确术语),既是单引号又是双引号。除了这个列表还有其他的吗?

#include <stdio.h>

int improvedCountWords(const char *str) {

int size=0;
int number=0;
int length=0;
while (str[size]!='\0'){
if (str[size]==' ' || str[size]=='.' || str[size]=='\\' || str[size]=='*' || str[size]=='"'){
if (length>0){
number+=1;
length=0;
}

}
else if (str[size]=='-' || str[size]=="'"){
if (length>0){
length++;
}

}
else{
length++;
}
size++;
}
if (length>0){
number++;
}
return number;
}


int main(){
char s1[]="Panting heavily, he continues his exercises -- grepping, installing new packages, logging in as root, and writing replacements for two-year-old shell scripts in Python.";

char s2[]="\" You'll know why Python is better than Perl... when you try to read your code *six* months from now ...\"";
char s3[]="With Yoda strapped to his back, Luke climbs up one of the many thick vines that grow in the swamp until he reaches the Dagobah statistics lab.";

int x1=improvedCountWords("Python");
int x2=improvedCountWords("Python is AWESOME");
int x3=improvedCountWords(s3);
int x4=improvedCountWords(s1);
int x5=improvedCountWords(s2);


printf("%d,%d,%d,%d,%d",x1,x2,x3,x4,x5);



return 0;
}

最佳答案

您的指针是 "'",int 是 str[size] 中的字符值,在 == 比较中被提升。就在这一行,没处理好;

    else if (str[size]=='-' || str[size]=="'"){

您可能需要像这样将它与 '\'' 进行比较;

    else if (str[size]=='-' || str[size]=='\''){

关于c - 警告 : Comparison between pointer and integer and escape characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910767/

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