C函数服用无效*函数参数 [英] C Functions taking void * function parameter
问题描述
早些时候,我有一个C结构
Earlier I had one structure C
typedef struct c
{
int cc;
}CS;
我用来调用库函数说INT的GetData(CS * X),这是我返回上述C结构的数据。
I used to call a library function say int GetData(CS *x) which was returning me the above structure C with data.
GetData(CS *x)
曾经是像函数调用:
Function call used to be like:
CS CSobj;
GetData(&CSObj);
现在有两种结构说C和D
Now there are two structures say C and D
typedef struct c
{
int cc;
}CS;
CS CSobj;
typedef struct d
{
int dc;
int dd;
}DS;
DS DSobj;
功能的GetData()已经被修改为的GetData(无效* x)的。
我要调用库函数说INT的GetData(无效* X),这将返回我的上述结构中的一个通过void *的参数中。
该函数的返回类型告诉返回其结构。
Function GetData() has been modified to GetData(void* x). I have to call the library function say int GetData(void* x) which will be returning me one of the above structures through that void* paramter. The return type of the function tells which structure is returned.
问题是当调用函数的GetData()如何以及传递什么参数,因为我不知道该结构体函数将返回。
任何方式解决这一问题的?
Problem is while calling the function GetData() how and what parameter to pass since I am unaware of which struct the function will return. Any way out of this problem?
推荐答案
您可以使用一个联盟
// define union of two structs
union DorC {
DS DSobj;
CS CSobj;
} CorD;
// call using union
rc = GetData(&CorD);
if (rc == Ctype) {
// use CorD.CSobj;
} else {
// use CorD.DSobj;
}
警告未经测试的未检查语法code!
Warning untested un syntax checked code!
这篇关于C函数服用无效*函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!