gpt4 book ai didi

c - 返回值的类型检查

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:53 24 4
gpt4 key购买 nike

我有一个列表,我希望能够在其中放置不同的类型。我有一个函数返回索引处的当前值:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com