我正在尝试将子字符串转换为整数,但我得到了这个输出
int main()
{
char buf[50] = "210-567-12-2040-34567890.txt";
char* pStart = buf;
char* pCurrent = buf;
while(*pCurrent != '\0')
{
if (*pCurrent == '-' || *pCurrent == '.')
{
uint32_t val = strtoul(pStart, NULL, 10);
pStart = pCurrent+1;
printf("%ul\n",val);
}
++pCurrent;
}
return 0;
}
我得到这个输出
210l
567l
12l
2040l
34567890l
为什么里面有一个l
?
最佳答案
这里的一些答案正是您正在寻找的:What is the difference between char s[] and char *s?
简短的回答是,作为函数参数,两者之间没有区别。您可以将 char* pStart
变量传递到 strtoul
中,就像将 char str[20]
变量传递到其中一样。这将使它直接从 pStart
指向的任何内存地址读取字符串,无需先复制到数组中。
关于将计算字符串转换为 uint32_t,而不将其复制到 c 中的 char[] 缓冲区中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49868094/