【发布时间】:2019-09-20 19:28:40
【问题描述】:
我有 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 中不存在泛型类型,所以我认为我可以使用我的结构的大小来使用指针算术。可以的话帮忙吗?
【问题讨论】:
-
如果对数组进行排序并不重要,您可以简单地使用标准库中的
bsearch(以及可选的qsort)。