我有一个列表,我希望能够在其中放置不同的类型。我有一个函数返回索引处的当前值:
void *list_index(const List * list, int index) {
assert(index < list->size);
return list->data[index];
}
数组中有多种类型,例如:
typedef struct structA { List *x; char *y; List *z; } structA;
typedef struct structB { List *u; char *w; } structB;
现在为了从数组中获取数据:
structA *A;
structB *B;
for(j=0... ) {
A = list_index(list, j);
B = list_index(list, j);
}
但是现在我如何找出返回值的类型呢?这可以用 typeof 实现吗(顺便说一句,我正在使用 GCC)?
这是否可能,或者我是否必须进行某种不同的构造?
最佳答案
您必须使用如图所示的 union here .
关于c - 返回值的类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1779483/