How to assign enum to an enumeration variable

I have the following code in C:

typedef enum
{
   MONDAY = 0,
   TUESDAY,
   WEDNESDAY
} Weekday;

typedef struct
{
   int age;
   int number;
   Weekday weekday;
} Info;

typedef struct
{
   int age;
   Weekday weekday;
} Data;

Info info;
Data data;
info.weekday = data.weekday;

lint gives the following error:

info.weekday = data.weekday; Type mismatch (assignment) (enum/enum)

How to assign an enumeration to another enumeration variable?

+4
source share
2 answers

Your code is not incorrect, it simply indicates a potential error. this error will not be displayed if compiled with the C ++ compiler, where it will save type information.

The problem is that in C, once you assign an enum value, it becomes an int type. So, as soon as you read from a weekday, its type is now int not Weekday.

A way around the error reported in lint by encoding is to force it to be assigned again from enum in the same way.

void SetWeekday( Info *info, Data* data )
{
     switch (Data->Weekday)
     {
         case MONDAY: info->Weekday = MONDAY;
         case TUESDAY: info->Weekday = TUESDAY;
         etc...
     }
}

lint , , .

( / ..)

0

:

MONDAY = 0;

, .

: , "lint".

, . . , ( lint?).

, , , :

typedef enum Weekday
{
    //...
} Weekday;

, , int :

typedef struct
{
    // ...
    int weekday;
} Info;

.

0

All Articles