2016-07-31 169 views
1

我是新来的,这是我的第一个问题。strstr函数不返回NULL

我已经搜索过这个问题,但没有找到这种类型的东西。 所以就这样了。

我正在写一个名称数据库的菜单程序,使用2d字符数组。以下是我的主要功能。

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函数总是返回true,即使我提供了一些乱码名称。 我使用的是Ubuntu 16.04 gcc 5.3.1

我试着用断点调试strstr,它正确地接收了两个字符串,但总是将指针返回干草堆。

__strstr_sse2 (haystack_start=0x7fffffffdcb0 "Imtiyaz", needle_start=0x7fffffffdc60 "abcd") at ../string/strstr.c:53 

53 ../string/strstr.c:没有这样的文件或目录。

草堆为"Imtiyaz"和针是"abcd"

,这里是它返回什么。

find (p=0x7fffffffdcb0) at name_data.c:126 

我不明白这里发生了什么问题,它是从我身边?

还有一件事,以前我尝试过使用strcasestr(),但是编译器会抛出一个警告“隐式声明”,尽管我正确地包含了<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。

+0

两个'if'语句有一个尾部';'不应该在那里。代码应该在调用'scanf()'时检查错误,并且在编译时调用'fgets()' – user3629249

+0

时,始终启用所有警告消息。然后修复这些警告。 – user3629249

回答

8

有在

if (strstr(p[i], str) != NULL); 

末虚假;因此,if声明不做任何动作,下一个语句break;始终执行。

find函数中有2个此错误发生。

+0

这样一个愚蠢的错误。无论如何,谢谢你指出。 –

1

strcasestr()是非标准的,只有定义的,如果你做到这一点,在源文件的最顶端,它使用它:

#define _GNU_SOURCE 

至于为什么strstr()不是做你的期望,你将有把你的程序烧得更多,看看有什么不对。就目前而言,有很多代码,其中包括一些代码(例如count的增加),这些代码根本没有显示。

+0

Thnx回复。编辑包含其他功能。看看。 –