C语言程序一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 14:10:37
void location(int y,int x) //打印记录消息
{
int i=0,len;
char str[][50]={"1.Load from default file","2.Load from other file","0.Retrun to Main Menu"};

while(strcmp(str[i],"stop"))
{
set_point(y+i,x);
printf("%s",str[i]);
fflush(stdout);
i++;

}
}

这哪里有毛病,要的内容是打出来了,接下来就是一堆乱码

while(strcmp(str[i],"stop"))这句有问题~

原型:extern int strcmp(char *s1,char * s2);
用法:#include <string.h>
功能:比较字符串s1和s2。
说明:
当s1<s2时,返回值<0
当s1=s2时,返回值=0
当s1>s2时,返回值>0
所以只要反回的不是0,也就是s1和s2不完全匹配i就一直向后自加,您的字符串数组中没有stop,导致内存溢出,输出乱码

可以加入stop
char str[][50]={"1.Load from default file","2.Load from other file","0.Retrun to Main Menu","stop"};

或者循环条件加一个检验str是否超出边界