Solved

Enum declaration

Posted on 2003-10-29
6
480 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

821 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