Solved

Enum declaration

Posted on 2003-10-29
6
478 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now