Solved

Enum declaration

Posted on 2003-10-29
6
481 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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