0

I have enum in header file.

typedef enum{
     up = 8, down = 2, left = 4, right = 6
}direction;

I want use enum to identify type of move. Like this:

void sayTypeOfMove(int type){

     switch(type){
          case direction.up:
                 printf("IT IS UP MOVE...");
                 break;
     }

}

The code does not compile , where is the problem?

4
  • 1
    direction.up.....:-) it's not a structure member, after all... Commented Jul 9, 2015 at 7:45
  • 3
    Even for a simple question like this, where the problem is obvious considering the code, you should always provide the actual (complete and unedited) errors you get when posting a question regarding build errors. Commented Jul 9, 2015 at 7:48
  • 3
    As for your problem, I think that looking for a nice book or tutorial might be a good idea. Commented Jul 9, 2015 at 7:49
  • Before you ask your next question, you probably mean '2', '4', '6' and '8' not 2,4,6, and 8 for your direction_t enums, assuming you are using a numeric keypad and getchar() or equivalent ... otherwise use switch(direction-'0'){ ... sorry I just can't bring myself use type as a variable name Commented Jul 9, 2015 at 7:57

2 Answers 2

5

C understands the enum elements when it knows you're dealing with that enum, so the right code would be

void sayTypeOfMove(direction type){

     switch(type){
          case up:
                 printf("IT IS UP MOVE...");
                 break;
     }

}

By the way, type is a really bad name, because it feels so much like it should be a reserved keyword.

Sign up to request clarification or add additional context in comments.

Comments

2

With a definition like

typedef enum{
     up = 8, down = 2, left = 4, right = 6
}direction;

direction is a type, it's not a variable.

You need to define a variable of that type, and then use the value.

And remember, the enum does not have a member variable access concept, at all. The enumerator list contains "enumeration-constant" s. You can just use them directly, as a value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.