0

I've updated search function in helpers.c with code below. I had to remove reference to helpers.h since that was causing even more errors.

    #include <cs50.h>
    // #include "helpers.h"
    
    bool search (int value, int values[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if(values[i] == value)
            {
                return true;
            }
            else
            {
                return false;
            }  
        }
      
    }
    
    void sort(int values[], int n)
    {
        // TODO: implement an O(n^2) sorting algorithm
        return;
    }

When compiling helpers.c I receive following error. I played with curly brackets by removing them but couldn't figure out what's the problem is. Please help.

    jharvard@ubuntu (~/pset3/find): make helpers
    clang -ggdb3 -O0 -std=c99 -Wall -Werror    helpers.c  -lcs50 -lm -o helpers
    helpers.c:18:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
    }
    ^
    1 error generated.
    make: *** [helpers] Error 1

UPDATE 1:

    #include <cs50.h>
    // #include "helpers.h"
    
    bool search (int value, int values[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if(values[i] == value)
            {
                return true;
            }
        }
        return false;
    }
    
    void sort(int values[], int n)
    {
        // TODO: implement an O(n^2) sorting algorithm
        return;
    }

Now I receive following error:

    jharvard@ubuntu (~/pset3/find): make helpers
    clang -ggdb3 -O0 -std=c99 -Wall -Werror    helpers.c  -lcs50 -lm -o helpers
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
    /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
    /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [helpers] Error 1

What's my mistake?

UPDATE 2:

Here are my code for:

helpers.h

    #include <cs50.h>
    
    bool search(int value, int values[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if(values[i] == value)
            {
                return true;
            }
        }
        return false;
    }
    
    void sort(int values[], int n);

helpers.c

    #include <cs50.h>
    #include "helpers.h"
    
    bool search (int value, int values[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if(values[i] == value)
            {
                return true;
            }
        }
        return false;
    }
    
    void sort(int values[], int n)
    {
        return;
    }

and finally find.c

    #include <cs50.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "helpers.h"
    
    const int MAX = 65536;
    
    int main(int argc, string argv[])
    {
        if (argc != 2)
        {
            printf("Usage: ./find needle\n");
            return -1;
        }
    
        int needle = atoi(argv[1]);
    
        int size;
        int haystack[MAX];
        for (size = 0; size < MAX; size++)
        {
            printf("\nhaystack[%d] = ", size);
            int straw = GetInt();
            if (straw == INT_MAX)
            {
                break;
            }
         
            haystack[size] = straw;
        }
        printf("\n");
    
        sort(haystack, size);
    
        if (search(needle, haystack, size))
        {
            printf("\nFound needle in haystack!\n\n");
            return 0;
        }
        else
        {
            printf("\nDidn't find needle in haystack.\n\n");
            return 1;
        }
    }

and I'm receiving following error, when compiling find.c

    jharvard@ubuntu (~/pset3/find): make find
    clang -ggdb3 -O0 -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm
    helpers.c:4:6: error: redefinition of 'search'
    bool search (int value, int values[], int n)
         ^
    ./helpers.h:15:6: note: previous definition is here
    bool search(int value, int values[], int n)
         ^
    1 error generated.
    make: *** [find] Error 1
12
  • If I am not mistaken, you suppose to not compile the helpers.c separately but run make find command, since helpers.c is only a part of your code in find.c.
    – user2477
    Commented Jan 28, 2015 at 13:20
  • Yes, you're right I need to compile find.c. I already tried and didn't succeed. find.c references to helpers.c and since helpers.c has faults, then find.c won't work too.
    – Dauren Z
    Commented Jan 29, 2015 at 14:32
  • Can you show the output of the make find command?
    – user2477
    Commented Jan 29, 2015 at 14:48
  • Should I have identical content of search function in helpers.c and helpers.h?
    – Dauren Z
    Commented Jan 29, 2015 at 14:59
  • 3
    Possible duplicate of Pset3 Linear Search (control may reach end of non-void function) Commented Dec 28, 2016 at 13:54

1 Answer 1

0

I am not sure but i think the error is caused because the compiler is considering all the branches the program can take during the execution. In this case considers the condition in the for loop i < n, this condition can be false if n is zero or negative. If this happen the for loop is never going to execute. An since the function per its declaration it is supposed to return a bool, like the error says control may reach the end of the function without returnig any bool, since the return commands are all inside the for loop.

Now you can think that n is never going to be zero or negative because is the size of an array, but the compiler doesn't know that and considers all options.

Another thing is that your function as it is right now is not working properly, because only checks the first element of the array and returns true or false.

You must log in to answer this question.

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