I\'ve been getting strange compiler errors on this binary search algorithm. I get a warning that control reaches end of non-void function
. What does this mean?<
add to your code:
"#include < stdlib.h>"
return EXIT_SUCCESS;
at the end of main()
It means it's searching for a function that needs to be completed.
else if(val == sorted[mid]) return mid;
so, remove the if() part and modify the code or add an else() at the end which returns an int.
I had the same problem. My code below didn't work, but when I replaced the last "if" with "else", it works. The error was: may reach end of non-void function.
int shifted(char key_letter)
{
if(isupper(key_letter))
{
return key_letter - 'A';
}
if(islower(key_letter) //<----------- doesn't work, replace with else
{
return key_letter - 'a';
}
}
Make sure that your code is returning a value of given return-type irrespective of conditional statements
This code snippet was showing the same error
int search(char arr[], int start, int end, char value)
{
int i;
for(i=start; i<=end; i++)
{
if(arr[i] == value)
return i;
}
}
This is the working code after little changes
int search(char arr[], int start, int end, char value)
{
int i;
int index=-1;
for(i=start; i<=end; i++)
{
if(arr[i] == value)
index=i;
}
return index;
}
The compiler isn't smart enough to know that <
, >
, and ==
are a "complete set". You can let it know that by removing the condition "if(val == sorted[mid])" -- it's redundant. Jut say "else return mid;
"
The compiler cannot tell from that code if the function will ever reach the end and still return something. To make that clear, replace the last else if(...)
with just else
.