在C中宣布全球工会

And*_*ith 1 c global-variables unions

我不确定如何在C中声明一个全局联合.下面是我的代码(所有这些都在main之外).

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;
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我收到错误:

test.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘find’.
Run Code Online (Sandbox Code Playgroud)

如何以一种我可以将它用作函数的返回类型的方式声明联合?

先感谢您!安德鲁

eph*_*ent 5

你错了.

有一个DictValtypedef,但你试图DictValue在定义上使用.