在C中,是否有一种快速方法可以检查单个数组值与任何其他数组值之间的相等性?

时间:2021-12-31 21:25:01

I've been trying to find how to do this for hours. Is there a simple way to check if the value of an array is equal to any of the values in another array that has been dynamically allocated?

我一直试图找到如何做这几个小时。有没有一种简单的方法来检查数组的值是否等于另一个已动态分配的数组中的任何值?

I don't have any real example code since my code is unable to do this regardless of what I've tried, but the general idea I'm trying to figure is:

我没有任何真正的示例代码,因为无论我尝试过什么,我的代码都无法做到这一点,但我想要的一般想法是:

if *whatsread[i] == dictionary[any value by checking all]//so if the current value  
                                          matches any value found in the dictionary array
    {
    execute this code;
    }

It seems so deceivingly simple I can't believe there isn't a simple way to do this. I've searched all over and haven't found a solid answer. I'm open to anything.

这看起来如此简单,我无法相信没有一种简单的方法可以做到这一点。我已经搜遍过,并没有找到一个可靠的答案。我对任何事都持开放态度。

Also, the both arrays are char types and every value is a string.

此外,两个数组都是char类型,每个值都是一个字符串。

1 个解决方案

#1


3  

You can use the function for linear searching lfind for this:

您可以使用该函数进行线性搜索lfind:

size_t nel = 0;
char *f = lfind(whatsread[i], dictionary, &nel, DICT_SIZE, (int(*)(const void*, const void*))strcmp);
if (f) {
     // f points to the entry 
} else {
     // Not found
}

#1


3  

You can use the function for linear searching lfind for this:

您可以使用该函数进行线性搜索lfind:

size_t nel = 0;
char *f = lfind(whatsread[i], dictionary, &nel, DICT_SIZE, (int(*)(const void*, const void*))strcmp);
if (f) {
     // f points to the entry 
} else {
     // Not found
}