开发者

Use of enums in C?

Is there anyway by which we can copy one enum to another one?For eg:

     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 not can anyone please show me the way to copy the values of enum from one to another?

I was getting an error while executing this like

  1. incompatible types in assignment of Element3_Range*' toElement3_Ra开发者_JS百科nge[10]'
  2. cannot convert Element4_Range' toElement3_Range' in assignment

Thanks and regards

Maddy


Cast it:

Source = (Element3_Range)Target;


An enum is an int with type checking. If you do not want the type checking, use int.


You might also consider using a switch block:

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

It may not always be desired to use the "actual value" of an enum, as opposed to its "logical value". Of course, this isn't fast, but that's not the point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜