C函数采用void *函数参数

早些时候我有一个结构C.

typedef struct c { int cc; }CS; 

我曾经调用一个库函数说int getData(CS * x),它返回了我带有数据的上述结构C.

 GetData(CS *x) 

函数调用过去如下:

 CS CSobj; GetData(&CSObj); 

现在有两种结构,比如C和D.

 typedef struct c { int cc; }CS; CS CSobj; typedef struct d { int dc; int dd; }DS; DS DSobj; 

函数GetData()已被修改为GetData(void * x)。 我必须调用库函数说int getData(void * x),它将通过void * paramter返回上述结构之一。 函数的返回类型告诉返回哪个结构。

问题是在调用函数GetData()时如何以及要传递的参数,因为我不知道函数将返回哪个结构。 有什么办法摆脱这个问题?

你可以使用联盟

  // 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; } 

警告未经测试的未经语法检查的代码!