如何初始化在C通用功能 [英] how to initialize generic functions in c
问题描述
我试着在C通用的比较函数用法的一个简单的例子,但在初始化时,我得到的警告信息的类型时,初始化它们是不相容的。感觉,因为我失去了一些东西基本在这里...
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT
比较(INT(* COMP)(void *的第一,无效*秒),无效*改编,诠释大小)
{
INT I,J,的DUP = 0,arrsize = sizeof的(ARR)/尺寸;
对于(i = 0; I< arrsize;我++)
{
为(J = 0; J< arrsize; J ++)
{
如果(J!=ⅰ)
的DUP + =(* COMP)(安培;常用3 [I],和放大器;常用3 [J]);
}
}
返回的DUP;
}INT
compareints为(int *首先,为int *秒)
{
如果(*第一== *秒)
返回1;
返回0;
}INT主(INT ARGC,为const char * argv的[])
{
INT(* COMP)(void *的,无效*)=安培; compareints; //这里就是我得到的警告...
INT ARR [10] = {22,39,78,22,99,12,82,10,11,28};
的printf(以下简称ARR具有的DUP数:%d \\ n,比较(比较,编曲,sizeof的(INT)));
返回0;
}
编辑:
该方案不正常并且它有与阵列的处理做。对于功能前卫的修改是如下:
INT
比较(INT(* COMP)(void *的第一,无效*秒),无效**编曲,诠释大小)
....
....
....
的DUP + =(* COMP)(ARR [I],编曲[J]);INT
compareints(....)
如果((*((INT *)第一))==(*((INT *)二)))
返回1;
INT
主(INT ARGC,为const char * argv的[])
{
INT(* COMP)(void *的,无效*)=安培; compareints;
INT ARR [10] = {22,39,78,22,99,12,82,10,11,28};
无效** ARR2 =的malloc(sizeof的为(int *)* 10);
的for(int i = 0;我小于10;我++)
{
ARR2 [I] =安培;常用3 [I]
}
的printf(以下简称ARR具有的DUP数:%d \\ n,比较(比较,ARR2,10));
返回0;
}
不幸的是,你不能混用无效*
和为int *
这样当他们一个函数签名的一部分。尽管为int *
可转换为无效*
, INT(*)为(int * ,为int *)
不能转换为 INT(*)(void *的,无效*)
。签名必须完全匹配。
INT
compareints(void *的第一,无效*秒)
{
如果(*((INT *)第一)== *((INT *)第二))
返回1;
返回0;
}
Im trying a simple example of the usage of a generic comparison-function in c but when initializing it I get the warning message that the types are incompatible when initializing them. Feels as I am missing something basic here...
#include <stdio.h>
#include <string.h>
int
compare(int (*comp)(void *first, void *second), void *arr, int size)
{
int i, j, dups=0, arrsize=sizeof(arr)/size;
for(i=0; i<arrsize; i++)
{
for(j=0; j<arrsize; j++)
{
if(j!=i)
dups+=(*comp)(&arr[i], &arr[j]);
}
}
return dups;
}
int
compareints(int *first, int *second)
{
if(*first==*second)
return 1;
return 0;
}
int main(int argc, const char * argv[])
{
int (*comp)(void *, void *)=&compareints; //Here is where I get the warning...
int arr[10]={22, 39, 78, 22, 99, 12, 82, 10, 11, 28};
printf("The arr has dups: %d\n", compare(comp, arr, sizeof(int)));
return 0;
}
EDIT: the program was not functioning properly and it had to do with the handling of the array. The modifications for functioning prog are below:
int
compare(int (*comp)(void *first, void *second), void **arr, int size)
....
....
....
dups+=(*comp)(arr[i], arr[j]);
int
compareints(....)
if((*((int *)first))==(*((int *)second)))
return 1;
int
main(int argc, const char * argv[])
{
int (*comp)(void *, void *)=&compareints;
int arr[10]={22, 39, 78, 22, 99, 12, 82, 10, 11, 28};
void **arr2=malloc(sizeof(int *)*10);
for(int i=0; i<10; i++)
{
arr2[i]=&arr[i];
}
printf("The arr has dups: %d\n", compare(comp, arr2, 10));
return 0;
}
Unfortunately you can't mix void *
and int *
like this when they're part of a function signature. Even though int *
can be converted to void *
, int (*)(int *, int *)
can't be converted to int (*)(void *, void *)
. The signatures have to match exactly.
int
compareints(void *first, void *second)
{
if (*((int *) first) == *((int *) second))
return 1;
return 0;
}
这篇关于如何初始化在C通用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!