Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by c_code
    The only error message the debugger gives me is :
    " 1>c:\users\mo\documents\visual studio 2010\projects\prac5task1.c\prac5task1.c\prac5task1.c(92): warning C4715: 'findNumber' : not all control paths return a value"

    Im not too sure what that means...
    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:
    Code:
        else /* Performs a linear search on the unsorted array */
        {
            for (index = 0;index < size; ++index)
            {
                if (a[index] == searchKey)
                {
                    return index;
                }
            }
    
        }
    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.

    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:
    Code:
        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;
                    return index;
                    found = 1;
                }
    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.

    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

    • c_code agrees : Good explanation
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0
    Thanks so much! I will give your construct a try

    Can you please just tell me what "int*a" means , is it a pointer?
  4. #18
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    I tend to remove white space in places that others don't.

    int*a;

    int *a;

    "a is a pointer to integer."


    This is also relevant, but you don't know it yet,
    Code:
    int (*compare)(void*,void*);
    
    /*
    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 11:28 AM. Reason: insert close code tag.
    [code]Code tags[/code] are essential for python code and Makefiles!
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo