在C中声明全局工会

问题描述:

我不确定如何在C中声明全局联合。下面是我的代码(全部都在main之外)。在C中声明全局工会

typedef union{ 
    int iVal; 
    char* cVal; 
} DictVal; 
struct DictEntry{ 
    struct DictEntry* next; 
    char* key; 
    DictVal val; 

    int cTag; 
}; 

DictVal find(char* key); 

int main() 
{ 
    struct DictEntry dictionary[101]; 
    //printf("Hello"); 
} 

DictValue find(char* key) 
{ 
    DictVal a; 
    a.iVal = 3; 
    return a; 
} 

有了这个,我收到错误:

test.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘find’. 

我怎样才能宣布联合的方式,我可以用它作为返回类型为函数?

预先感谢您! Andrew

+0

不应该是 typedef union DictVal int int iVal; char * cVal; } DictVal; ? – blueberryfields 2011-01-31 04:08:39

+2

你确定吗?单独的代码片段编译得很好。 – ephemient 2011-01-31 04:09:29

您输入错了。

有一个DictVal typedef,但您试图在定义上使用DictValue

拼写错误。

您宣布:

typedef union{ 
    int iVal; 
    char* cVal; 
} DictVal; 

,但要使用

DictValue find(char* key) 
{ 
    DictVal a; 

与DictVal更换DictValue。

也使主返回的东西。通常应该是0.

上帝保佑!