Imtiyaz Shaikh Imtiyaz Shaikh - 1 month ago 7
C Question

strstr function not returning NULL

I'm new here and this is my first question.

I have already searched for this issue, but not find anything of this kind.
So here it goes.

I'm writing a menu based program for a name database, using 2d array of characters. below is my main function.

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");
}
}
}


Input and print function is working fine, but I have problem in find function.
Here it is.

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
is a global variable here, which is incremented in input function.
The
strstr
function always return true, even if I supply some gibberish name.
I am using ubuntu 16.04 gcc 5.3.1

I tried debugging with a breakpoint at
strstr
, it receives both the strings correctly, but always return the pointer to haystack.

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


53 ../string/strstr.c: No such file or directory.

Haystack is
"Imtiyaz"
and needle is
"abcd"


and here is what it returns.

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


I dont understand what is going wrong here, is it from my side ?

and one more thing, previously I tried using
strcasestr()
, but compiler throws a warning "implicit declaration", though I included
<string.h>
properly.

please help me.

Edit: Ok friends, I will show the input and print function too to let you people analyze my program properly. Which are, by the way working fine.

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");
}


I haven't implemented other functions yet(like delete, search etc.) as you can see int switch-case.

Answer

There is a spurious ; at the end of

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

Hence the if statement does nothing and the next statement break; is always executed.

There are 2 occurrences of this bug in the find function.

Comments