为什么这个程序调试不起来?

来源:百度知道 编辑:UC知道 时间:2024/05/25 14:18:39
#include<stdio.h>
int find(char *s,int num)
{
int maxlen=0,curlen=0;
num=0;
for(;*s!='\0';)
{
while(*s==' ')
s++;
while(*s!=' ')
{
curlen++;
s++;
}
if(curlen>maxlen)
{
maxlen=curlen;
num=1;
}
else
if(curlen==maxlen)
num++;
}
return maxlen;
}
void main( )
{
char str[200];
int number,len;
gets(str);
len=find(str,number);
printf("maxlen=%d, number is %d\n",len,number);
}
没有错啊,可就是调试不起来!!!
number 好象不需要初始化吧?让它等于零也还是调不出来~
要怎么修改呢

1.

find()函数中的while(*s!=' ')应改为while(*s!=' '&&*s!='\0')

2.
find()函数的参数num应该设为指针类型,以便将值带回主函数中,

3.
find()函数中的变量curlen的值应该在每次for(;*s!='\0';)开始时置0,

修正后的find()函数如下:

int find(char *s,int *num)
{
int maxlen=0,curlen=0;

*num=0;
for(;*s!='\0';)
{curlen=0;
while(*s==' ')
s++;
while(*s!=' '&&*s!='\0')
{
curlen++;
s++;
}
if(curlen>maxlen)
{
maxlen=curlen;
*num=1;
}
else
if(curlen==maxlen)
(*num)++;
}
return maxlen;
}

在主函数中调用时应该是以下形式:

len=find(str,&number);

'gets': This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

uninitialized local variable 'number' used

你这个不就是在字符