我尝试编写一个函数,该函数在数据结构中查找空指针。函数必须将void*转换为任何类型的结构。
假设我编写了一个结构,它以空指针的形式存储在我的数据结构中。然后调用该函数,该函数输出所有存储的数据元素的信息。要做到这一点,函数必须知道它应该转换到哪种类型。
所以我的问题是:是否可以以某种形式以参数的形式给函数提供它所需要的信息?
示例代码:
typedef struct{
int a, b
} teststruct;
void DSOut(datastructure* ds, datatypeinfo dt){
//...
//search for data in ds
//...
//if data is found cast it to the type defined in dt
//and print out the a and b fields
}
int main(){
datastructure* ds = DSCreate(4, 3); //can hold any type of data,
//but should hold just one at a time
//4 and 3 are just example parameters
teststruct ts;
ts.a = 4;
ts.b = 10;
teststruct ts2;
ts2.a = 6;
ts2.b = 12;
//Add the teststructs to the data-structure
DSAdd(2, 2, ts); //the numbers are just for example
DSAdd(4, 1, ts2);
datatypeinfo dt = teststruct; //stores the type teststruct for DSOut
DSOut(ds, dt); //function, that prints information of all added teststructs
return 0;
}在本例中,DSOut(x,y)应该打印以下内容:
- on position 2, 2 is an element which holds following data: 4, 10.
- on position 4, 1 is an element which holds following data: 6, 12.你觉得这有可能吗?
发布于 2014-12-27 17:42:58
类型不能在C中作为参数传递,所以对您的问题的简短回答是“不,它不能完成”,至少在一般情况下不是这样。您可以传递一些内容,允许您识别有限的一组类型中的一种,然后通过硬代码来处理每种类型(我正在考虑一个大的switch语句)。由于您没有指定datatypeinfo的外观,所以不清楚您期望它有多普遍。
发布于 2014-12-27 18:10:32
我可以考虑在struct中添加一个类型标识符字段,并检查它的值以决定如何打印它,并使用函数初始化结构以处理type字段
enum Types {
Point3D,
Point2D
};
struct Base {
enum Types type;
};
struct Point3D {
enum Types type;
int x;
int y;
int z;
};
struct Point2D {
enum Types type;
int u;
int v;
};
void print(void *data)
{
switch (((struct Base *)data)->type)
{
case Point2D:
{
struct Point2D *point;
point = (struct Point2D *)data;
printf("2D: %d, %d\n", point->u, point->v);
}
break;
case Point3D:
{
struct Point3D *point;
point = (struct Point3D *)data;
printf("3D: %d, %d, %d\n", point->x, point->y, point->z);
}
break;
}
}
void initialized2dPoint(struct Point2D *const point, int u, int v)
{
if (point == NULL)
return;
point->type = Point2D;
point->u = u;
point->v = v;
}
void initialized3dPoint(struct Point3D *const point, int x, int y, int z)
{
if (point == NULL)
return;
point->type = Point3D;
point->x = x;
point->y = y;
point->z = z;
}
int main(void)
{
struct Point2D point2d;
struct Point3D point3d;
initialized2dPoint(&point2d, 1, 2);
initialized3dPoint(&point3d, 3, 4, 5);
print(&point2d);
print(&point3d);
return 0;
}https://stackoverflow.com/questions/27669777
复制相似问题