求大神指教一下这个程序的问题。
/*已知学生库char *name[6]={"Join","Mary", "Lily", "Bob", "Williams", "White"};要求根据输入学生姓名查找该学生是第几个。*/
程序1用的是数组编的,可以正常运行;程序2使用指针编的,但是键入字符串回车后,程序就出问题,无法继续执行。请高手指点一下程序2的错误。
程序1:#include "stdio.h"
void main()
{
int n=0;
char name[6][20]={"Join","Mary", "Lily", "Bob", "Williams", "White"},c[20];
int funa(char str1[],char str2[]);
printf("Input a name:");
gets(c);
while(n<6)
{
if(funa(name[n],c))
{
printf("The name is number %d!",n+1);break;
}
n++;
}
if(n==6)printf("There is not this name!");
}
int funa(char str1[],char str2[])
{
int n;
for(n=0;str1[n]!='\0';n++)
if(str1[n]!=str2[n])return 0;
return 1;
}
程序2:#include "stdio.h"
void main()
{
int n=0;
char *name[6]={"Join","Mary", "Lily", "Bob", "Williams", "White"},*c;
int funa(char *str1,char *str2);
printf("Input a name:");
gets(c);
while(n<6)
{
if(funa(name[n],c))
{
printf("The name is number %d!",n+1);break;
}
n++;
}
if(n==6)printf("There is not this name!");
}
int funa(char *str1,char *str2)
{
int n;
for(n=0;*(str1+n)!='\0';n++)
if(*(str1+n)!=(*(str2+n)))return 0;
return 1;
}