Using enums in C?

Is there a way that we can copy one enum to another? For instance:

     enum Element4_Range{a4=1,b4,c4,d4};
     enum Element3_Range{a3=1,b3,c3};
     enum Element3_Range Myarr3[10];
     enum Element4_Range Myarr4[10];

     enum Element3_Range MyFunc(Element4_Range);

     main()
     {
          MyFunc(Myarr4);
     }
     enum Element3_Range MyFunc(Element4_Range Target)
     {
           enum Element3_Range Source;
           Source = Target;-----------Is this possible?
      }

      

If anyone please show me a way to copy enum values ​​from one to another?

I was getting error when doing this, for example

  • incompatible types when assigning Element3_Range*' to

    Element3_Range [10] '
  • cannot convert Element4_Range' to

    Element3_Range 'to assignment

Thanks and respect Maddy

+2


a source to share


3 answers


Paste it:



Source = (Element3_Range)Target;

      

+2


a source


An enumeration is a type-checked int. If you don't need type checking, use int.



+1


a source


You can also use a switch box:

switch(Target) {
case a4:
  return a3;
case b4:
  return b3;
case c4:
  return c3;
}

      

It is not always possible to use the "actual value" of an enumeration rather than its "boolean value". Of course, this is not fast, but it is not.

0


a source







All Articles