0

This is my code for the helper.c file. The code forms part of the 'find' program. I'm getting an error: 'expected expression' re: the last 'else' statement. I have tried removing the 'else' statement or removing the 'false' from the second 'return' but this doesn't fix it. I can't see what I'm doing wrong.

bool search(int value, int values[], int n)
{

    if (n>=0)
    {
       for (int i=0; i<n; i++ )
       {

           if (values[i] == value)
           {
              return true;
           }

           else    
           { 
           return false;    
           }
    }
    else
    {
        return false;
    }
}

}

After advice I've changed lower part to this:

            else if ((values[i] != value) && (i = n-1))   
            {
                return false;    
            }
        }
        return 0;
    }
}

Now I'm getting one error which is:

control may reach end of non-void function

}

^

////////////

Third attempt, was successful.

bool search(int value, int values[], int n)
{

    if (n >= 0)
    {
        for (int i=0; i<n; i++)
        {
            if (values[i] == value)
            {
                return true;
            }
        }
    }    
    return false;

}

1 Answer 1

1

If you adjusted the indentation to match the curly braces, you'd realise that you put the else after the for loop, not one level higher after the if.

You don't need that outer if, as your for loop would do the same test. But it won't hurt either.

You have a logical problem inside your for loop, as you always return in the first iteration. Don't stop the search on a mismatch.

5
  • Thanks. I see what you mean about the logical problem so I've tried something else. Please look now at modified question (lower part).
    – Android1
    Commented Mar 27, 2017 at 8:07
  • Testing for n-1 is possible (you'd use == to compare, = is assignment!), but would not be my preferred solution (I'd return true inside the loop on a match, but return false only after the loop, which means only mismatches inside). Your problem is probably the placement of the return 0; (which is equivalent to return false;, but please don't mix those). There is at least one execution path (not necessarily reachable) which ends the function without ever reaching a return statement, making the returned value undefined (whatever was in that memory space before).
    – Blauelf
    Commented Mar 27, 2017 at 8:25
  • Blauelf: Thanks for your advice. Much simplified now and passes checks and compiles as well as the sort function.
    – Android1
    Commented Mar 27, 2017 at 8:57
  • The loop won't run if n<0 (i is initially 0, so the test i < n would fail), you don't have to check that with an additional if. But that's only three lines less.
    – Blauelf
    Commented Mar 27, 2017 at 9:14
  • Blaueif: Thanks. As you can probably tell I've only been learning C for a few weeks. You've been a big help.
    – Android1
    Commented Mar 27, 2017 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .