strstr 函数不返回 NULL
strstr function not returning NULL
我是新来的,这是我的第一个问题。
我已经搜索过这个问题,但没有找到任何此类内容。
就这样吧。
我正在为名称数据库编写基于菜单的程序,使用二维字符数组。下面是我的主要功能。
int main(void)
{
char name[MAX][25] = { 0 };
char choice;
while (1) {
printf("******************menu****************\n");
printf("i: input\n");
printf("p: print\n");
printf("f: find\n");
printf("d: delete\n");
printf("s: sort\n");
printf("e: edit\n");
printf("q: quit\n");
printf("**************************************\n\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
getchar();
switch (choice) {
case 'i':
input(name);
break;
case 'p':
print(name);
break;
case 'f':
find(name);
break;
case 'q':
return 0;
default:
printf("Invalid choice\n");
}
}
}
输入和打印功能正常,但查找功能有问题。
在这里。
void find(char (*p)[25])
{
int i;
char str[25];
if (count == 0) {
printf("Empty database\n");
return;
}
printf("Enter name to search: ");
fgets(str, 25, stdin);
str[strlen(str) - 1] = 0; //Removing new line character at the end of string.
for (i = 0; i < count; i++) {
if (strstr(p[i], str) != NULL); //Breaking the loop, when first occurence is found among all the names.
break;
}
if (i == count) {
printf("Not found\n"); // if loop is not terminated by "break" statement,
// that means strstr returned NULL for all names.
return;
}
printf("Names matching with %s\n", str);
for (i = 0; i < count; i++) {
if (strstr(p[i], str) != NULL); // Again looping to print all the matching names.
puts(p[i]);
}
}
count
这里是一个全局变量,在输入函数中自增。
strstr
函数总是 return 为真,即使我提供了一些乱七八糟的名字。
我正在使用 ubuntu 16.04 gcc 5.3.1
我尝试在 strstr
处使用断点进行调试,它正确地接收了两个字符串,但总是 return 指向 haystack 的指针。
__strstr_sse2 (haystack_start=0x7fffffffdcb0 "Imtiyaz", needle_start=0x7fffffffdc60 "abcd") at ../string/strstr.c:53
53 ../string/strstr.c: 没有那个文件或目录。
干草堆是 "Imtiyaz"
针是 "abcd"
这就是它 returns.
find (p=0x7fffffffdcb0) at name_data.c:126
我不明白这里出了什么问题,是我这边的问题吗?
还有一件事,之前我尝试使用 strcasestr()
,但编译器抛出警告 "implicit declaration",尽管我正确地包含了 <string.h>
。
请帮助我。
编辑:好的朋友,我也会展示输入和打印功能,让大家正确分析我的程序。顺便说一下,哪些工作正常。
void input(char (*p)[25])
{
if (count == MAX) {
printf("Memory full\n");
return;
}
printf("Enter name: ");
fgets(p[count], 25, stdin);
p[count][strlen(p[count]) - 1] = 0; //Removing the new line character at the end of string.
count++;
}
void print(char (*p)[25])
{
int i;
if (count == 0) {
printf("Empty database\n");
return;
}
printf("********************************\n");
for (i = 0; i < count; i++) {
printf("%d %s\n", i + 1, p[i]); //printing names with serial numbers.
}
printf("********************************\n");
}
我还没有实现其他功能(如删除、搜索等),如您所见,int switch-case。
strcasestr()
是非标准的,仅当您在使用它的源文件的最顶部执行此操作时才定义:
#define _GNU_SOURCE
至于为什么 strstr()
没有按照您的预期进行,您将不得不进一步简化您的程序以查看问题所在。就目前而言,有大量代码,包括一些您根本没有显示的代码(例如 count
的递增)。
末尾有一个虚假的 ;
if (strstr(p[i], str) != NULL);
因此 if
语句什么都不做,下一个语句 break;
总是被执行。
find
函数中出现了 2 次此错误。
我是新来的,这是我的第一个问题。
我已经搜索过这个问题,但没有找到任何此类内容。 就这样吧。
我正在为名称数据库编写基于菜单的程序,使用二维字符数组。下面是我的主要功能。
int main(void)
{
char name[MAX][25] = { 0 };
char choice;
while (1) {
printf("******************menu****************\n");
printf("i: input\n");
printf("p: print\n");
printf("f: find\n");
printf("d: delete\n");
printf("s: sort\n");
printf("e: edit\n");
printf("q: quit\n");
printf("**************************************\n\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
getchar();
switch (choice) {
case 'i':
input(name);
break;
case 'p':
print(name);
break;
case 'f':
find(name);
break;
case 'q':
return 0;
default:
printf("Invalid choice\n");
}
}
}
输入和打印功能正常,但查找功能有问题。 在这里。
void find(char (*p)[25])
{
int i;
char str[25];
if (count == 0) {
printf("Empty database\n");
return;
}
printf("Enter name to search: ");
fgets(str, 25, stdin);
str[strlen(str) - 1] = 0; //Removing new line character at the end of string.
for (i = 0; i < count; i++) {
if (strstr(p[i], str) != NULL); //Breaking the loop, when first occurence is found among all the names.
break;
}
if (i == count) {
printf("Not found\n"); // if loop is not terminated by "break" statement,
// that means strstr returned NULL for all names.
return;
}
printf("Names matching with %s\n", str);
for (i = 0; i < count; i++) {
if (strstr(p[i], str) != NULL); // Again looping to print all the matching names.
puts(p[i]);
}
}
count
这里是一个全局变量,在输入函数中自增。
strstr
函数总是 return 为真,即使我提供了一些乱七八糟的名字。
我正在使用 ubuntu 16.04 gcc 5.3.1
我尝试在 strstr
处使用断点进行调试,它正确地接收了两个字符串,但总是 return 指向 haystack 的指针。
__strstr_sse2 (haystack_start=0x7fffffffdcb0 "Imtiyaz", needle_start=0x7fffffffdc60 "abcd") at ../string/strstr.c:53
53 ../string/strstr.c: 没有那个文件或目录。
干草堆是 "Imtiyaz"
针是 "abcd"
这就是它 returns.
find (p=0x7fffffffdcb0) at name_data.c:126
我不明白这里出了什么问题,是我这边的问题吗?
还有一件事,之前我尝试使用 strcasestr()
,但编译器抛出警告 "implicit declaration",尽管我正确地包含了 <string.h>
。
请帮助我。
编辑:好的朋友,我也会展示输入和打印功能,让大家正确分析我的程序。顺便说一下,哪些工作正常。
void input(char (*p)[25])
{
if (count == MAX) {
printf("Memory full\n");
return;
}
printf("Enter name: ");
fgets(p[count], 25, stdin);
p[count][strlen(p[count]) - 1] = 0; //Removing the new line character at the end of string.
count++;
}
void print(char (*p)[25])
{
int i;
if (count == 0) {
printf("Empty database\n");
return;
}
printf("********************************\n");
for (i = 0; i < count; i++) {
printf("%d %s\n", i + 1, p[i]); //printing names with serial numbers.
}
printf("********************************\n");
}
我还没有实现其他功能(如删除、搜索等),如您所见,int switch-case。
strcasestr()
是非标准的,仅当您在使用它的源文件的最顶部执行此操作时才定义:
#define _GNU_SOURCE
至于为什么 strstr()
没有按照您的预期进行,您将不得不进一步简化您的程序以查看问题所在。就目前而言,有大量代码,包括一些您根本没有显示的代码(例如 count
的递增)。
;
if (strstr(p[i], str) != NULL);
因此 if
语句什么都不做,下一个语句 break;
总是被执行。
find
函数中出现了 2 次此错误。