如何在c中传递Union数组

时间:2012-10-07 10:14:17

标签: c

我有以下数据类型声明。

    typedef struct{
            int aa;
    }A;

    typedef struct{
        int bb;
    }B;

    typedef struct{

        union {
                A a;
                B b;
        }*D;

        int other;
    }myType;

    //Now I want to pass the array "D" and the variable "other" to a function 
    // that will use them to construct "myType"
    // How can I pass D as parameter? what whill be its type?

    //I want to have a function like the following with "?" filled. 

        callFunction(? d, int other){
            //construct "myType"
            myType typ;
            typ.D     = d;
            typ.other = other; 
        }

我试图在“mytype”struct 之外声明 union ,然后在中使用 D * d; mytype“struct 在这种情况下,我有这个错误

错误:'D'之前的预期说明符限定符列表

代码如下:         // struct A和B在上面声明

    union {
            A a;
            B b;
    }D;

    typedef struct{            
            D* d;            
            int other;
    }myType;

任何帮助都会很明显,

感谢。

1 个答案:

答案 0 :(得分:1)

typedef union {
   A a;
   B b;
} D;

typedef struct{
   D d;
   int other;
}myType;

callFunction(D *d, int other)

union D {
   A a;
   B b;
};

typedef struct{
   union D d;
   int other;
}myType;

callFunction(union D *d, int other)

callFunction的正文对于两者都是相同的:

{
            //construct "myType"
            myType typ;
            typ.D     = *d;
            typ.other = other; 
}