首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将void*转换为C中的可变数据类型

将void*转换为C中的可变数据类型
EN

Stack Overflow用户
提问于 2014-12-27 17:37:38
回答 2查看 1.3K关注 0票数 0

我尝试编写一个函数,该函数在数据结构中查找空指针。函数必须将void*转换为任何类型的结构。

假设我编写了一个结构,它以空指针的形式存储在我的数据结构中。然后调用该函数,该函数输出所有存储的数据元素的信息。要做到这一点,函数必须知道它应该转换到哪种类型。

所以我的问题是:是否可以以某种形式以参数的形式给函数提供它所需要的信息?

示例代码:

代码语言:javascript
复制
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)应该打印以下内容:

代码语言:javascript
复制
- 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.

你觉得这有可能吗?

EN

回答 2

Stack Overflow用户

发布于 2014-12-27 17:42:58

类型不能在C中作为参数传递,所以对您的问题的简短回答是“不,它不能完成”,至少在一般情况下不是这样。您可以传递一些内容,允许您识别有限的一组类型中的一种,然后通过硬代码来处理每种类型(我正在考虑一个大的switch语句)。由于您没有指定datatypeinfo的外观,所以不清楚您期望它有多普遍。

票数 1
EN

Stack Overflow用户

发布于 2014-12-27 18:10:32

我可以考虑在struct中添加一个类型标识符字段,并检查它的值以决定如何打印它,并使用函数初始化结构以处理type字段

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27669777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档