C从特定函数指针转换为不太特定的函数指针

C从特定函数指针转换为不太特定的函数指针,c,function-pointers,C,Function Pointers,假设我有以下C语言代码: void general(void (*function)(void *something), void *something); void funct_type1(struct something1 *something); void funct_type2(struct something2 *something); general(funct_type1, something1); general(funct_type2, something2); 如何在没有指

假设我有以下C语言代码:

void general(void (*function)(void *something), void *something);
void funct_type1(struct something1 *something);
void funct_type2(struct something2 *something);
general(funct_type1, something1);
general(funct_type2, something2);
如何在没有指针转换警告和手动抑制某些错误的情况下编译此文件


稍后编辑:我尝试不修改funct_type1()和func_type2()定义。

正确的修复方法是将特定于实现的内容折叠到抽象接口后面的函数中,即:

void funct_type1(void *s1)
{
 struct something1 *s = s1;
 /* ... rest of function ... */
}

否则,您将不得不在调用
general()

时强制转换函数指针。正确的解决方法是将特定于实现的内容折叠到抽象接口后面的函数中,即:

void funct_type1(void *s1)
{
 struct something1 *s = s1;
 /* ... rest of function ... */
}

否则,您将不得不在调用
general()

时强制转换函数指针,您可以使用
void*

void general(void (*function)(void *something), void *something);
void funct_type1(void* something);
void funct_type2(void* something);
general(funct_type1, &test1);
general(funct_type2, &test2);
在哪里

或者可以使用
联合
对结构进行分组

#include <stdio.h>

struct something1
{
    int a;
};

struct something2
{
    int b;
};

union something3
{
    struct something1 s1;
    struct something2 s2;
};

void general(void (*function)(union something3 *something), union something3* something)
{
    if (function != NULL)
        function(something);
}

void funct_type1(union something3* something)
{
    something->s1.a = 1;

    printf("%s - something->s1.a = %d\n", __func__, something->s1.a);
}

void funct_type2(union something3* something)
{
    something->s2.b = 3;

    printf("%s - something->s2.b = %d\n", __func__, something->s2.b);
}

int main()
{
    union something3 test1;
    union something3 test2;

    test1.s1.a = 0;
    test2.s2.b = 0;

    general(funct_type1, &test1);
    general(funct_type2, &test2);

    printf("%s - test1.s1.a = %d\n", __func__, test1.s1.a);
    printf("%s - test2.s2.b = %d\n", __func__, test2.s2.b);

}
#包括
结构某物1
{
INTA;
};
结构某物2
{
int b;
};
联合总会
{
结构1 s1;
结构2 s2;
};
void-general(void(*函数)(并集某物3*某物),并集某物3*某物)
{
if(函数!=NULL)
功能(某物);
}
无效函数类型1(并集某物3*某物)
{
某物->s1.a=1;
printf(“%s-something->s1.a=%d\n”,\uuuu func\uuuuu,something->s1.a);
}
无效函数类型2(并集某物3*某物)
{
something->s2.b=3;
printf(“%s-something->s2.b=%d\n”,\uuuu func\uuuuu,something->s2.b);
}
int main()
{
联合某物3试验1;
联合某物3试验2;
test1.s1.a=0;
test2.s2.b=0;
一般(功能类型1和测试1);
一般(功能类型2和测试2);
printf(“%s-test1.s1.a=%d\n”、\uuu func\uuuu、test1.s1.a);
printf(“%s-test2.s2.b=%d\n”、\uuu func\uuuu、test2.s2.b);
}

您可以使用
void*

void general(void (*function)(void *something), void *something);
void funct_type1(void* something);
void funct_type2(void* something);
general(funct_type1, &test1);
general(funct_type2, &test2);
在哪里

或者可以使用
联合
对结构进行分组

#include <stdio.h>

struct something1
{
    int a;
};

struct something2
{
    int b;
};

union something3
{
    struct something1 s1;
    struct something2 s2;
};

void general(void (*function)(union something3 *something), union something3* something)
{
    if (function != NULL)
        function(something);
}

void funct_type1(union something3* something)
{
    something->s1.a = 1;

    printf("%s - something->s1.a = %d\n", __func__, something->s1.a);
}

void funct_type2(union something3* something)
{
    something->s2.b = 3;

    printf("%s - something->s2.b = %d\n", __func__, something->s2.b);
}

int main()
{
    union something3 test1;
    union something3 test2;

    test1.s1.a = 0;
    test2.s2.b = 0;

    general(funct_type1, &test1);
    general(funct_type2, &test2);

    printf("%s - test1.s1.a = %d\n", __func__, test1.s1.a);
    printf("%s - test2.s2.b = %d\n", __func__, test2.s2.b);

}
#包括
结构某物1
{
INTA;
};
结构某物2
{
int b;
};
联合总会
{
结构1 s1;
结构2 s2;
};
void-general(void(*函数)(并集某物3*某物),并集某物3*某物)
{
if(函数!=NULL)
功能(某物);
}
无效函数类型1(并集某物3*某物)
{
某物->s1.a=1;
printf(“%s-something->s1.a=%d\n”,\uuuu func\uuuuu,something->s1.a);
}
无效函数类型2(并集某物3*某物)
{
something->s2.b=3;
printf(“%s-something->s2.b=%d\n”,\uuuu func\uuuuu,something->s2.b);
}
int main()
{
联合某物3试验1;
联合某物3试验2;
test1.s1.a=0;
test2.s2.b=0;
一般(功能类型1和测试1);
一般(功能类型2和测试2);
printf(“%s-test1.s1.a=%d\n”、\uuu func\uuuu、test1.s1.a);
printf(“%s-test2.s2.b=%d\n”、\uuu func\uuuu、test2.s2.b);
}

函数指针从一种函数指针类型转换为另一种类型是形式上未定义的行为。事实上,这种转换可能在很多系统上都能工作——编译器甚至可能以确定性的方式将它们作为非标准扩展来实现——但这不是推荐的做法。从一种函数指针类型到另一种函数指针类型的函数指针转换是形式上未定义的行为。事实上,这种转换可能在很多系统上都能工作——编译器甚至可能以确定性的方式将它们实现为非标准扩展——但这不是推荐的做法。