Solved

Enum declaration

Posted on 2003-10-29
6
482 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 25 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 150 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 70
Header of docx file 17 132
Microsoft C++ code failing in executable that worked 9 123
Precision Problem in C++ 7 51
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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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