- 我已经编写了一个字符串长度的代码。
- 编译成功,但存在一些问题 输出。
- 输入字符串后,我的输出卡住了。
- 我必须使用指针找到长度
#include<stdio.h>
int leng(char*);
void main()
{
char str[20];
scanf("%s",str);
printf("%s",str);
int l;
l=leng(str);
printf("%d",l);
}
int leng(char*p)
{
int l=0;
while(*p!='\0')
{
l++;
}
return l;
}
最佳答案
在您的代码中,问题就在这里
while(*p!='\0')
此处指针*p
不会递增。因此,while 循环无限工作,可能会导致程序崩溃。
所以,使用
while(*p++!='\0')
而不是
while(*p!='\0')
关于c - 查找字符串的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47110962/