I have this function that takes a char** and a char* as parameters and it’s supposed to return either the index of the char* in char** or return -1 if it’s not in the string array. The error I’m sure is the while(arr[i] != NULL)... but I’m not sure how else to do it.
int isInArray(char** arr, char* str) {
int i = 0;
while(arr[i] != NULL) {
if(strcmp(arr[i], str) == 0)
return i;
i++;
}
return -1;
}
You probably forgot to set the last
char*element ofarrto NULL. If you can’t do that, then you should introduce a third argumentsize_t arr_lento pass the length ofarr.Unrelated tip: since you’re not modifying
*stror*arr, be const-correct and use: