enum identifiers in a switch case

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

enum identifiers in a switch case

5,907 次查看
jah
Contributor I
the case of a switch expression having an enum type and the case expressions include the identifiers which are looked up as enum constants in the enum type does not build proper.
produces and error c1815 & c1845.


//from three different files code segmants
my_varriable.c
enum myenum {xx,yy,zz} MyEnum;

my_varriable.h
extern enum myenum MyEnum;

main.c
#include my_varriable.h
switch(MyEnum){
case xx:
case yy:
case zz:
};
标签 (1)
0 项奖励
回复
9 回复数

2,497 次查看
alex_spotw
Contributor III
HI:

I think you cannot used the 'MyEnum' identifier as the switch variable, because it is the enum type name. You need to create another variable and assign it the enum constant that you want to process with yout switch statement.

Regards,

Alex
0 项奖励
回复

2,497 次查看
jah
Contributor I
C is case sensative, hterefor the statement enum myenum MyEnum is functional. anyways in the actual code the actual varriables are not anything myenum / MyEnum
0 项奖励
回复

2,497 次查看
CompilerGuru
NXP Employee
NXP Employee
I guess the problem is that the xx,yy & zz identifiers are not visible in main.c.
I would propose to move the enum type definition into the header file.

my_varriable.c
#include "my_varriable.h"
myenumType MyEnum;

my_varriable.h
#ifndef MY_VARIABLE_H_
#define MY_VARIABLE_H_
typedef enum {xx,yy,zz} myenumType;
extern myenumType MyEnum;
#endif /* MY_VARIABLE_H_ */
0 项奖励
回复

2,497 次查看
admin
Specialist II
As an aside for maximum efficiency, remember that underlying data type for an enum is an int, for which the compiler might generate 16-bit signed code, which can be inefficient for 8-bit micros.  If you have only a few elements you might be better off just making a bunch of #define's and assigning them to a uchar.
 
-Tomahawk
 
 
0 项奖励
回复

2,497 次查看
CrasyCat
Specialist III
Hello
Or if you are using CodeWarrior Compiler tell it to use 8-bits to encode enums :smileywink:
 
CrasyCat
0 项奖励
回复

2,497 次查看
jah
Contributor I
"use CodeWarrior Compiler tell it to use 8-bits to encode enums"
thanks CrasyCat

this sounds interesting, where is the setting to do this??

THANKS!
0 项奖励
回复

2,497 次查看
CrasyCat
Specialist III
Hello
If you are using the IDE to build the application:
  - Open the project
  - Open the target setting dialog (Press ALT+F7)
  - switch to the "Compiler for HC08" Panel
  - Click on the "Type Sizes" button.
  - In the "Standard Types Setting" dialog set enum to 8-bit using the radio button
  - Close the dialog with OK
  - Close target setting dialog with OK.
 
If you are building from batch (or from am make file) add option -TE1 to your command line.
 
Note that you can also set enum type to be unsigned per default. The option for 8-bit unsigned enums is
-TE1uE
 
CrasyCat
0 项奖励
回复

2,497 次查看
admin
Specialist II


CrasyCat wrote:
Hello
If you are using the IDE to build the application:
  - Open the project
  - Open the target setting dialog (Press ALT+F7)
  - switch to the "Compiler for HC08" Panel
  - Click on the "Type Sizes" button.
  - In the "Standard Types Setting" dialog set enum to 8-bit using the radio button
  - Close the dialog with OK
  - Close target setting dialog with OK.
 
If you are building from batch (or from am make file) add option -TE1 to your command line.
 
Note that you can also set enum type to be unsigned per default. The option for 8-bit unsigned enums is
-TE1uE
 
CrasyCat



Crasy,
 
That just earned you a 5-star post rating.  :smileywink:
 
-Tomahawk
0 项奖励
回复

2,497 次查看
jah
Contributor I
THANKS: alex_spotw, Tomahawk, CrasyCat, CompilerGuru, EVERYONE!!
0 项奖励
回复