2
我有三个结构header
,dataA
和dataB
。 header
将确定将使用的结构。该dataA
和dataB
具有几乎相同的结构(比方说):指向两个不同的结构
struct dataA
{
int intValue;
char reserved1[8];
float floatValue;
char reserved2[4];
short shortValue;
};
struct dataA
{
int intValue;
short shortValue;
char reserved[2];
float floatValue;
};
我要打印喜欢:
sprintf(outStr, "%i, %f, %s", p->intValue, p->floatValue, p->shortValue);
- 或 -
sprintf(outStr, "%i, %f, %s", p.intValue, p.floatValue, p.shortValue);
我如何宣布p
? (注:两个dataA
和dataB
有一个大的结构,但几乎相同的数据,除了那些保留值)
我想是这样的:
void * p;
if (header->type==1)
p = (dataA*)(pData);
else if (header->type==2)
p = (dataB*)(pData);
// print all data here
注意:这里pData
是一个指向我将阅读的(原始)数据的指针。我只需要这些非保留值,而不考虑保留的值。
你能改变类型定义吗? – reuben