gpt4 book ai didi

将 ascii 码转换为整数

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

我已经知道如何转换了,但是“while”并没有停止,它一直在运行,我不知道为什么? “string !='\0' ”不起作用吗?字符总是以“\0”结尾吗?

#include <stdio.h>
int ascii_integer(char *string){
int result = 0;
while (string !='\0')
{
if (*string >='0' && *string <='9')
{
result *= 10;
result += *string - '0';
}
string++;
}
return result;
}
int main(){
char string[] = "12345";
int result = ascii_integer(string);
printf("%d\n",result);
}

最佳答案

线

while (string !='\0')

应该是

while (*string)

我很惊讶你没有收到编译器的警告

关于将 ascii 码转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24720386/

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