2
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>  

Is there some function in C which does similar thing to in_array in PHP?

2

1 Answer 1

6

Nope, but you can implement like this

typedef int (*cmpfunc)(void *, void *);

int in_array(void *array[], int size, void *lookfor, cmpfunc cmp)
{
    int i;

    for (i = 0; i < size; i++)
        if (cmp(lookfor, array[i]) == 0)
            return 1;
    return 0;
}

int main()
{
    char *str[] = {"this is test", "a", "b", "c", "d"};

    if (in_array(str, 5, "c", strcmp))
        printf("yes\n");
    else
        printf("no\n");

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.