Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enum declaration

Posted on 2003-10-29
6
Medium Priority
?
487 Views
Last Modified: 2011-09-20
I had a moment (?) of madness and wrote code like the following:


typedef enum {APPLE = 0, PEAR, ORANGE} Fruits;
typedef enum {TREE, FLOWER , GRASS} Plants;


void foo(void)
{
    Fruits fruit;
    Plants plant;
}

Now, clearly what i meant was:

typedef enum Fruits {APPLE = 0, PEAR, ORANGE} ;
typedef enum Plants {TREE, FLOWER , GRASS};

The code compiles, which I find odd, as i thought that the way i had declared it, Fruits and Plants were the names of variables of the type i had declared.

Whats going on?
0
Comment
Question by:mattjsimps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 9640612
Hey u r using the typedef

Whenever u use typedef, u r basically aliasing something

Thus if u say

typedef struct Stack {
  int a
} stk ;

Then u have declared a type called stk

While if u do not use typedef, then it would be a variable of the struct/enum

struct Stack {
  int a
} stk ;
stk would be a variable of type struct Stack.

Same is applicale for enum
0
 
LVL 1

Author Comment

by:mattjsimps
ID: 9640653
yeah, i know. My point is that i left out the type name, which is perfectly valid i thought. What confuses me is that the name i gave at the end of the declaration (eg Fruits) should have been the name of a <i>variable</i> , not a type. Yet i could use it as a type.

~Confused
0
 

Accepted Solution

by:
milanygandhi earned 600 total points
ID: 9640674
1.  The syntax of typedef is as follows:
            typedef oldtype newtype;
2.   The syntax of enum is as follows:
            enum type_name { value1, value2, ...}

typedef can be combined with definition of an enum type by replacing "oldtype" with the enum definition. i.e. following definition is valid
3.   typedef enum type_name {value1, value2, ...} newtype;

Further, while combining two definitions, "type_name" is optional as we are specifying the new name for the enum type, namely, "newtype".   Hence your first definition completely makes sense.  As the name suggests, typedef is used to define(or rename) types.  It can not be used to declare variables.

Another example of combining typedef is struct definition.
typedef struct{
   int a;
   float b;
   } mytype; //mytype is a  type

mytype data1,data2; //data1, data2 are variables





0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:mattjsimps
ID: 9640757
Ahhhh.

So, presumably, had i stated:

enum {APPLE, ORANGE} Fruits;

It wouldn't have compiled?
0
 
LVL 1

Author Comment

by:mattjsimps
ID: 9640767
Tryed it, and it doesn't.

Thank you very much
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9640778
Definitely,

That's what I said earlier

Whenever U use typedef, u are aliasing something

Thus when u use

typedef old new

u are saying that I would be using new instead of old

And in case of declaration, it aliases the type and a variable is not created
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question