c - 是否可以创建一个可以以不同结构运行的函数(用 C 语言)?

标签 c function pointers struct unions

我正在寻找用 C 语言创建一个函数,该函数允许我接收不同的结构类型作为参数。 例如,如果我创建 3 个不同的结构

struct a{
    struct datatype0{
         char test1[10];
    }datatype;
    struct a *next;
};

struct b{
    struct datatype1{
        int test1;
        char test2[20];
        int test3;
    }datatype;
    struct b *next;
};

struct c{
    struct datatype2{
        char test1;
        char test2;
        float test3;
        int test4;
        int test5;
    }datatype;
    struct c *next;
};

我想创建一个可以接收这三种不同结构之一作为参数的函数,因此我只能调用它来初始化第一种、第二种或第三种结构:

void function("---")//<-- inside the brackets i need to insert a parameter that can be struct a, or struct b or struct c.
{
//here for example I can insert the initialize function that have to work with any struct.

}

我尝试使用 union ,但我发现我必须为每种结构重新创建初始化函数...我尝试使用 void 指针,但我需要将这些指针转换为函数内部,并且我需要创建也为每种结构初始化函数...... 有什么想法吗??

最佳答案

总的来讲是:尽可能避免这样做,但要知道,如果确实需要的话,您可以将不同的结构传递给单个函数。

可能最简单的方法是创建一个包装结构,它包含 2 个成员:一个 union 和一个标志,让您知道传递了哪个结构。

typedef enum {
    A,
    B,
    C
} struct_type;

struct _wrapper {
    union {
        struct a A;
        struct b B;
        struct c C;
    };
    struct_type flag;
};

void my_function(struct _wrapper *data)
{
    switch (data->flag)
    {
        case A:
            struct a val = data.A;
            //do stuff with A
            break;
        case B:
            struct b val = data.B;
            break;
        case C:
            struct c val = data.C;
            //...
            break;
    }
}

另一种选择,尽管它被认为是不好的做法,并且最终会让您后悔,那就是依赖于任何结构的第一个成员的偏移量保证为 0 的事实。您可以将指针强制转换为任何struct 指向其第一个成员的指针。如果所有结构体的第一个成员都是兼容的,您可以依赖它(风险自负)。
利用此问题的一种方法是将函数指针设置为第一个成员,或者设置一个枚举字段,您可以将其用作标识结构的标志:

struct a {
    void (*common_member)();//
    /** other members **/
};
struct b {
    void (*common_member)();//needn't be the same name though
    /** other members **/
};

然后:

void my_func(void *data)
{//void pointer
    ((void (*)(void *))data)(data);//cast void *data to function pointer, and pass itself as an argument
}

如果结构体已正确初始化,并且成员指向正确的函数,那么这可以工作,但实际上要依赖的 if 太多了。

使用枚举作为第一个成员的风险稍小,但仍然不推荐。这是一种函数指针和 union 方法的组合

void my_func(void *data)
{
    //cast void * to struct_type *, dereference AFTER the cast
    //because you can't dereference a void *
    switch(*((struct_type *) data))
    {
        case A: /* stuff */ break;
        case B: /* struct b */ break;
    }
}

总而言之,使用第一种方法。不要使用函数指针成员,并承认第三种方法的本质:确实,您不需要包装器结构,但它并不比原始方法(函数指针)安全多少,而且也不比原始方法(函数指针)更冗长。第一种方法(使用 union )。

底线:结构和 union 是正确的选择

关于c - 是否可以创建一个可以以不同结构运行的函数(用 C 语言)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815651/

相关文章:

c++ - 按字母顺序排列结构数组的问题。从 Z 到 A 有效,但不能从 A 到 Z

c - 在 C 中“动态”使用变量?

javascript - Meteor 不等待函数的结果,返回未定义

pointers - "<type> is pointer to interface, not interface"困惑

c - Linux 中的可执行堆栈示例(i386 架构)

c - 在C编程中, “emit”做什么?

c++ - C中的这些声明有什么区别?

c - 如何使用 C 中的函数指针访问 .text 部分?

c - 运行时错误与编译时错误

c++ - 在 XCode 中桥接 C++ 和 Swift,正确的build设置是什么?