Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in array.c array_contains() doesn't work #28

Closed
gianmaria opened this issue Apr 4, 2016 · 7 comments
Closed

in array.c array_contains() doesn't work #28

gianmaria opened this issue Apr 4, 2016 · 7 comments

Comments

@gianmaria
Copy link

/**
 * Returns the number of occurrences of the element within the specified array.
 *
 * @param[in] ar the array that is being searched
 * @param[in] element the element that is being searched for
 *
 * @return the number of occurrences of the element
 */
size_t array_contains(Array *ar, void *element)
{
    size_t o = 0;
    size_t i;
    for (i = 0; i < ar->size; i++) {
        if (element == ar->buffer[i])  <------------ this line
            o++;
    }
    return o;
}

in array.c you are comparing only the addresses not the actual data, you need a comparison function as argument to array_contains
@srdja

@srdja
Copy link
Owner

srdja commented Apr 4, 2016

Technically it works, because comparing the pointer values was the original idea, but now that you mention it it may not be the most obvious. or the most useful thing to do. So yes, a comparator function should be included.

@pkill37
Copy link
Contributor

pkill37 commented Apr 4, 2016

Couldn't you just dereference the pointers and compare the values? What is the benefit of passing a comparator function? :)

@f2404
Copy link
Contributor

f2404 commented Apr 4, 2016

@faviouz It's not allowed to dereference a void * pointer.

@pkill37
Copy link
Contributor

pkill37 commented Apr 4, 2016

Ah, of course! :)

@f2404
Copy link
Contributor

f2404 commented Apr 5, 2016

Looks very similar to #18, right?

@gianmaria
Copy link
Author

@f2404 yes indeed

@srdja
Copy link
Owner

srdja commented Apr 11, 2016

I've added a contains variant that takes a comparison function. One should now be able to search for the actual values pointed to by the container elements.

@srdja srdja closed this as completed Apr 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants