I keep getting bad pointers. Can anyone tell me what am I doing wrong?
int SearchString( char* arr[], char* key, int size )
{
int n;
for ( n = 0; n < size; ++n ) {
if ( strcmp(arr[n], key) ) {
return n;
}
}
return -1;
}
char str[][16] = { "mov","cmp","add","sub","lea","not","clr","inc","dec","jmp","bne","red","jrn","psr","rts","stop"};
if(SearchString(str,"word",16) == -1){ return FALSE;}
Can't tell where your
word
originates from. You probably want toif (!strcmp(arr[n],key)) return n;
(the reverse). And the type of array is probably not what you want. Tryinstead. You have an array of arrays of characters and pass it where you actually expect an array of pointers.
strcmp()
returns zero if strings are equal! Your test should beif (!strcmp(...))
Also, consider using
strncmp()
.The parameter is passed as char **ar which is not correct.
One of the alternatives is changing protopype to:
int SearchString( char arr[][16], char* key, int size )
to get the expected behaviour.Change
char str[][16]
tochar *str[16]
(or onlychar *str[]
).Also,
strcmp
returns zero when the strings are equal, so you want this instead: