0

i wrote the search code that find.c calls from helpers.c and yet somehow the compiler keeps returning this results.

~/workspace/pset3/find/ $ make find
clang -ggdb3 -O0 -std=c11 -Wall -Werror -o find find.c helpers.c -lcs50 -lm
helpers.c:32:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
make: *** [find] Error 1

below is my search code but i cant seem to figure out the problem.

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm
    int i = 0;
    for(i = 0; i < n; i++)
    {
        int value = 0;
        int values[i];
        if(values[i] == value)
        {
            return true;
        }
        else
        return false;
    }
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm
    return;
}

2 Answers 2

0

Compilers are dumb. They only make straightforward checks of various conditions. In this case, it sees that if the for loop doesn't exist, which could possibly happen if n were 0, then the code would never hit a return statement before reaching the end of the search function. So, it doesn't like that.

For details, see How to solve "control may reach end of non void function" error

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1

Remove one curly brace after last return statement and place it before last return statement.

2
  • Close, but then the else clause and the curly brace pair should be removed completely. Dead code should always be removed. Yes, your solution works exactly right, but the goal here is also to teach, not to just provide the solution. Can you explain why your solution works? "Give a man a fish and feed him for a day. Teach him how to fish and feed him for a lifetime!" ;-)
    – Cliff B
    Commented Dec 10, 2016 at 20:08
  • Thanks. Your advice was helpful, Cliff B. Commented Jan 9, 2017 at 21:19

You must log in to answer this question.

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