泛型类型数组中的c搜索
我有两个结构泛型类型数组中的c搜索,c,arrays,search,C,Arrays,Search,我有两个结构 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 *ar
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;
}
如果我将函数的签名更改为:
short search_array(const foo *array, const int dim, int query)
以及调用方法来:
int x = search_array(foos, 3, 30);
但不适用于bar
struct和viceversa
我的目标是使用指针算法创建单个函数/过程,并且不存在代码重复。
我认为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;
}
short search_数组(const void*array、const int dim、const int item_size、int query)
{
短idx=-1;
如果(查询>=0)
{
对于(int i=0;i
旁注:您将数组大小作为
int
传递,但将索引作为short
传递到数组中。这些类型应该匹配,否则对于大小不适合short
的数组,代码将中断
同样,参数
query
的类型应该是unsigned short
,而不是int
,因为这是实际存储在结构中的内容。如果数组被排序无关紧要,您只需使用标准库中的bsearch
(以及可选的qsort
)即可;现在我将函数参数更改为size\t dim
和无符号短查询
。2个问题:我可以使用sizeof(foo)
而不是sizeof(foos[0])
?为什么要使用字符大小的单位?是因为char size是1吗?@user3744384是的,您当然也可以使用sizeof(foo)
。我选择了sizeof foos[0]
使其成为未来的证明。更改foos
的类型。对于第二个问题:由于sizeof
值以char
为单位,因此需要对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;
}