Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error passing union with enum on function

I am getting this error and dont know what to do :

error: request for member ‘Number’ in something not a structure or union
22 | if(answer==user.choice.Number)

The program chould check if the user input is equal to one of the enum elements and then print the number or the string in the union

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>
#include <string.h>

typedef enum{
    Number = 1,
    String = 2,
}eenum;


union data{
    int x;
    char str[10];
    eenum choice;
};


void display(union data user)
{
    int answer;
    puts("Enter1 or 2:");
    scanf("%d",&answer);
    if(answer==user.choice.Number)
    {
    user.x = 5;
    printf("%d\n",user.x);
    }
    else{
   strcpy(user.str,"hello");
     printf("%s\n",user.str);
    }
}

int main() {
union data user;
display(user);

    return 0;
}

>Solution :

Enums treated as global identifiers. You should access your enum like that:

enum input_type {
  NUMBER = 1,
  STRING = 2
}

...

if (answer == NUMBER) { ...

And for improve your understandings about enum, you should read this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading