Hello everyone.
Right now I am cursing the C language.
Why does it have to be so buggy?
Well here's my problem:
I have an array of pointers. The pointers point to strings. Printing the array yields the proper output.
I now want to SEARCH the array for certain strings.
If a match occurs, print certain strings in the array.
Simple, right?
Well, not so simple, because when I search for KNOWN strings(or rather their pointers) in the array, sometimes it finds them, sometimes not! Yet it should every time because I've printed the arrays values, and everything's in it's place!!
So why is "strstr()" being so picky?
Let's look at the code.
But first, let me show you a sample array printout:
___________________________________
line_ptrs[0] = "john ";
line_ptrs[1] = "peters ";
line_ptrs[2] = "texas";
line_ptrs[3] = "rick";
line_ptrs[4] = "jones";
line_ptrs[5] = "washington";
line_ptrs[6] = "sarah";
line_ptrs[7] = "doyle";
line_ptrs[8] = "texas";
line_ptrs[9] = "sally";
line_ptrs[10] = "ericson";
line_ptrs[11] = "washington";
.....etc.
___________________________________
Now for the code:
//////////////////////////// SNIPPET OF CODE ///////////////////
///////////////////////// END SNIPPET///////////////////////////////PHP Code:
char state[20];
int q=0;
int y;
printf("Search for all people in a certain State...\"1\" exits...");
fgets(state,19,stdin);
while( (strcmp(state,"\n")) && (q=atoi(state) != 1))
{
/// the above means "while user hasn't entered a "1" or a newline..."
for(y=0;y<max_num;y++) ///"max_num" defined elsewhere
{
if( strstr(line_ptrs[y],state) )
{
printf("%s\n",line_ptrs[y-2]); //print first name
printf("%s\n",line_ptrs[y-1]); //print last name
}
}
printf("Search for all people in a certain State...\"1\" exits...");
fgets(state,19,stdin);
}
So even if ten of the pointers are "texas" only three successful "strstr()" attempts might work!
Or if there are eight "washington"s, strstr() might find seven!!
AAAAAAAARGH!
This inconsistancy shows thoughout the execution of this routine.
Any ideas?