Search code examples
cglobal-variablesunions

Declaring global unions in C


I am unsure how to declare a global union in C. Below is my code (all of which is outside of 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;
}

With this, I receive the error:

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

How can I declare the union in a way that I can use it as a return type for a function?

Thank you in advance! Andrew


Solution

  • You've typo'ed.

    There's a DictVal typedef but you tried to use DictValue on the definition.