c 在泛型数组中搜索

标签 c arrays search

我有 2 个结构,

typedef struct
{
    unsigned short id;
    char name[10];
    char email[10];
} foo;

typedef struct
{
    unsigned short id;
    char test[10];
    float ok;
} bar;

并且我需要创建一个可重用函数/过程来在这些结构的两个数组中搜索值。

这是我的功能:

short search_array(const int *array, const int dim, int query)
{
    short idx = -1;

    if (query >= 0)
    {
        for (int i = 0; i < dim; i++)
        {
            /*
             * since array is a generic pointer,
             * take:
             *  pointer to the position array+ i
             *  convert the value to unsigned short
             *  finally get final value of short
             */
            if ((*(unsigned short *) (array + i)) == query)
            {
                idx = i;
                i = dim;
            }
        }
    }

    return idx;
}

这是我的主要内容:

int main()
{
    foo a = {10, "ok", "pippo"};
    foo b = {50, "si", "gino"};
    foo c = {30, "si", "peppo"};

    foo foos[3] = {a, b, c};

    bar a1 = {6, "mario", 5.5};
    bar b2 = {56, "mimmo", 0};
    bar c3 = {69, "maronno", 9};

    bar bars[3] = {a1, b2, c3};

    int x = search_array((const int *) foos, 3, 50);
    int x1 = search_array((const int *) foos, 3, 9999999);

    int y = search_array((const int *) bars, 3, 69);
    int y1 = search_array((const int *) bars, 3, 9999999);

    return 0;
}

如果我将函数的签名更改为:它适用于 foo 结构:

short search_array(const foo *array, const int dim, int query)

和调用方法:

int x = search_array(foos, 3, 30);

但不适用于 bar 结构,反之亦然。

我的目标是通过使用指针算法创建单个函数/过程并且没有代码重复。 我认为 C 中不存在泛型类型,所以我认为我可以使用我的结构的大小来使用指针算法。如果可能,请帮忙?

最佳答案

您需要将结构的大小传递给函数,并对 char 大小的单元进行指针运算:

short search_array(const void *array, const int dim, const int item_size, int query)
{
    short idx = -1;

    if (query >= 0)
    {
        for (int i = 0; i < dim; i++)
        {
            /*
             * since array is a generic pointer,
             * take:
             *  pointer to the position array+ i
             *  convert the value to unsigned short
             *  finally get final value of short
             */
            if ((*(unsigned short *) ((const char*)array + i * item_size)) == query)
            {
                idx = i;
                i = dim;
            }
        }
    }

    return idx;
}


int main()
{
    foo a = {10, "ok", "pippo"};
    foo b = {50, "si", "gino"};
    foo c = {30, "si", "peppo"};

    foo foos[3] = {a, b, c};

    bar a1 = {6, "mario", 5.5};
    bar b2 = {56, "mimmo", 0};
    bar c3 = {69, "maronno", 9};

    bar bars[3] = {a1, b2, c3};

    int x = search_array(foos, 3, sizeof foos[0], 50);
    int x1 = search_array(foos, 3, sizeof foos[0], 9999999);

    int y = search_array(bars, 3, sizeof bars[0], 69);
    int y1 = search_array(bars, 3, sizeof bars[0], 9999999);

    return 0;
}

[Live example]


旁注:您将数组大小作为 int 传递,但数组的索引作为 short 传递。这些类型应该匹配,否则您的代码将因大小不适合 short 的数组而中断。

同样,参数query 的类型应该是unsigned short 而不是int,因为这是结构中实际存储的内容。

关于c 在泛型数组中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949669/

相关文章:

c - C 中的结构体赋值

javascript - Action Script 3. 检查数组是否有元素不跳转,然后跳转

datetime - solr搜索时间范围内的时间

java - 线性搜索多个整数

谁能告诉我这有什么问题吗?

c - (C编程)创建数组并分配空间(通过malloc)影响另一个数组

c - C语言的吃 bean 游戏

javascript - ES6 类上的自定义类数组 getter

c - 如何用 C 中的内存地址初始化 char 数组?

postgresql - 搜索土耳其语字符