March 27th, 2013, 10:58 AM
It means exactly what it says, that there is at least one control path that does not return a value. The function is declared to return a size_t, so it must return a value. Consider your else:
Originally Posted by c_code
What value is returned if the search fails? More importantly, in the unsorted case, where do you return that failure value? You don't. That is a control path that does not return a value, which is what the warning says.
else /* Performs a linear search on the unsorted array */
for (index = 0;index < size; ++index)
if (a[index] == searchKey)
You could explicitly return -1 after the for-loop, since the only way you would get there would be if a match was not found. While multiple return's in a function is common practice in C, the rules of structured programming that clifford referred to require that a function have only one exit point, hence only one return. While structured programming isn't The Word of God, following its precepts (eg, no goto's) does promote good practices and helps to keep you from shooting yourself in the foot. When you find a match, save the index and stop the search (eg, by using a break statement), then return that saved index at the end of the function.
To illustrate the point of using structured programming, here's a problem in your code caused by improper use of multiple returns:
Because you return, the following statement will never be executed. If your compiler were strict enough, it should have warned you about "unreachable code". While this does no actual harm, it makes the variable, found, extraneous; found serves no purpose. Now, if you just save the index of the match and then return it at the bottom of the function (meaning that you have removed and replaced all the other returns), then found would serve the very useful purpose of breaking you out of the search loop because of a match.
if (sort==1) /*Performs a binary search on the sorted array */
while ( (low <= high) && (found == 0) )
middle = (low + high) / 2;
if (searchKey == a[middle] )
index = middle;
found = 1;
This would be a good time for you to stop and read your function for content (just as you would read a message here for the information it contains). Think about what it does. Your use of index is different in the if and the else blocks. In the else, you use it to control the for loop, thus losing the default -1 value, but in the if block you use other variables to index into the array saving the matching index to index before returning it (why not just return middle?). Decide upon a consistent approach and apply it.
And why a size_t? An array index would not be a size_t, but rather an int, so why not an int? Yes, I'm sure that size_t typedef's to int or maybe to unsigned int, but there's a reason for that typedef that I don't see applying here.
Comments on this post
March 27th, 2013, 02:19 PM
Thanks so much! I will give your construct a try
Can you please just tell me what "int*a" means , is it a pointer?
March 27th, 2013, 02:58 PM
I tend to remove white space in places that others don't.
"a is a pointer to integer."
This is also relevant, but you don't know it yet,
compare is a pointer. Because *compare is parenthesized.
(*compare) is a function because that's the binding order,
(*compare) is a function taking two pointers to void as arguments.
And finally, it returns int.
compare is a pointer to a function returning int.
And the function (*compare) takes 2 arguments, each a pointer to void.
Last edited by b49P23TIvg; March 28th, 2013 at 12:28 PM.
Reason: insert close code tag.
[/code] are essential for python code and Makefiles!