C 函数的意外输出
我正在编写一个函数,用于从给定的字符串中查找字符: Test.c文件如下所示:C 函数的意外输出,c,linux,C,Linux,我正在编写一个函数,用于从给定的字符串中查找字符: Test.c文件如下所示: #include<stdio.h> int findIndex(char *array, char search) { int count=0; char test; int check = 0, repeat; if(search == ',') repeat = rand()%10; else repeat = rand()
#include<stdio.h>
int findIndex(char *array, char search)
{
int count=0;
char test;
int check = 0, repeat;
if(search == ',')
repeat = rand()%10;
else
repeat = rand()%11;
while((test = array[count]) != NULL)
{
if(test == search)
{
if(check == repeat)
return count;
check++;
}
count++;
}
}
int main()
{
char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
char testchar;
printf("Enter the search character: ");
testchar = getc(stdin);
printf("The search char found at: %d position.\n",findIndex(sc,testchar));
fflush(stdin);
while(testchar != 'N')
{
printf("The search char found at: %d posotion.\n",findIndex(sc,testchar));
printf("Enter the search character: ");
scanf("%c",&testchar);
fflush(stdin);
}
return 0;
}
我哪里出错了?
请告诉我问题所在以及解决方法。您可能希望主功能如下所示:
int main()
{
char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
char testchar;
printf("Enter the search character: ");
scanf("%c",&testchar);
while(testchar != '\n' && getchar() != '\n'); # discard any next chars
while(testchar != 'N') {
printf("The search char found at: %d position.\n",findIndex(sc,testchar));
printf("Enter the search character: ");
scanf("%c",&testchar);
while(testchar != '\n' && getchar() != '\n'); # discard any next chars
}
return 0;
}
上面的示例生成以下结果:
$ > ./a.out
Enter the search character: Y
The search char found at: 66 position.
Enter the search character: .
The search char found at: 0 position.
Enter the search character: ,
The search char found at: 83 position.
Enter the search character: g
The search char found at: 0 position.
Enter the search character: N
$ >
PS:fflush()不是由ISO为输入流定义的。它在Microsoft的C运行时库中定义,但不可移植。您的代码中有一条警告,必须更正
while((test = array[count]) != NULL)
上面的行生成警告:指针和整数之间的比较
while((test = array[count])!='\0')
在您的输出中,您将获得两次字符索引,因为换行字符也会被获取,并且它的位置也会被打印出来(当您按下enter键时)
为了避免这种情况,你可以这样做
while(testchar != 'N')
{
if(testchar!='\n'){//checking whether character is newline
printf("The search char %d found at: %d posotion.\n",testchar,findIndex(sc,testchar));
printf("Enter the search character: ");
scanf("%c",&testchar);
}
else
{
scanf("%c",&testchar);//reading the newline character but not performing any operation(printing the index )
}
}
就是这样,你的程序现在可以运行了
注意:使用fflush(stdin)被认为是一种不好的做法,并且具有未定义的行为。最好不要使用它来刷新输入流,因为它不可移植。请参阅此详细信息
输出
Enter the search character: Y
The search char found at: 66 position.
The search char 89 found at: 128 position.
Enter the search character: J
The search char 74 found at: 0 position.
Enter the search character: o
The search char 111 found at: 85 position.
Enter the search character: N
希望有帮助。祝你编码愉快 您是否尝试过在编译时启用警告(例如,-Wall)?我怀疑您会看到一个关于函数返回值的警告。您还会看到一个关于将
char
与指针进行比较的警告,特别是“不同级别的间接寻址”。我调试了您的源代码。它还使用换行符(当您按enter键时)所以它印刷了两次。谢谢大家的评论和建议。
Enter the search character: Y
The search char found at: 66 position.
The search char 89 found at: 128 position.
Enter the search character: J
The search char 74 found at: 0 position.
Enter the search character: o
The search char 111 found at: 85 position.
Enter the search character: N