473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

difference between "typedef enum" and "enum"

Hi all,

What will be difference between "typedef enum" and "enum".
or
difference between “typedef structure" and "structure"

I am going through some code.
in that some place they are using enum without typedef. In some places they are using typedef before enumeration definition.

Kindly provide some reference, for why they are using typedef.

Thanks and regards,
Sukumar
Nov 27 '07 #1
4 171243
Well it is relevent to C language in which if you use an enum or structure which are user defined data types, you need to specify the enum or struct keyword while using it.
For example you define a struct as
struct myStruct
{
int a;
char c;
}

Now when you want to use it you will do as

struct myStruct s;
s.a = 10;
s.c = 'b';

to avoid writing struct keyword again and again you can use typedef as

typedef struct myStruct
{
int a;
char c;
}


Now you can use it as

myStruct s;
s.a = 10;
s.c = 'b';


But C++ you don't require to do typedef even. Each user defined data type C++ can be used by the name used while defining it.
Nov 27 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
A typedef merely gives a new name to an existing type or a new name for a pointer. The idea in C is to increase portability. The same applies in C++ but also is expanded to provide a long name for template specialization.

Expand|Select|Wrap|Line Numbers
  1. typedef int BYTE;
  2.  
This typedef allows BYTE to be used as a type. You can change the entire body of code to use a long rather than an int just by changing the typedef.

Expand|Select|Wrap|Line Numbers
  1. typedef char* String;
  2.  
This typedef allows you to use String as a type rather than use char*:
Expand|Select|Wrap|Line Numbers
  1. String Label = "Made in China";
  2.  
In C for structs you can:
Expand|Select|Wrap|Line Numbers
  1. struct x
  2. {
  3.      int month;
  4.      int day;
  5.      int year;
  6. }Date;
  7.  
  8. typedef Date* DatePtr;
  9.  
This makes Date a keyword and pDate a keyword. Now you could:
Expand|Select|Wrap|Line Numbers
  1. Date dt;
  2. Dateptr pd = &dt;
  3.  
In C without the Date keyword you have to code:
Expand|Select|Wrap|Line Numbers
  1. struct Date dt;
  2. Dateptr pd = &dt;
  3.  
so the keyword avoids having to code struct. This is a non-issue in C++ since you don't have to code struct anyway. You would still need the typedef of Date* to DatePtr.

typedef is also used with function pointers:
Expand|Select|Wrap|Line Numbers
  1. typedef int (*Compare)(const char*, const char*);
  2.  
This makes Compare the type for the function pointer:
Expand|Select|Wrap|Line Numbers
  1. Compare cmp = strcmp;
  2.  
Now you can use Compare in your code instead of strcmp. The advantage here is that you can change the compare function to another function just by changing the typedef.

Generally, typedef is not used with enums. An enum is a list of named integer values so all you have to do is use the named value:
Expand|Select|Wrap|Line Numbers
  1. enum Value {BLACK, RED};
  2. int main()
  3. {
  4.    int data = 20;
  5.    if (data == BLACK)
  6.    {
  7.       //do something
  8.    }
  9. }
  10.  
By using the enum as a type you can:
Expand|Select|Wrap|Line Numbers
  1. enum Value {BLACK, RED};
  2. int main()
  3. {
  4.    Value data = 20;   //ERROR 20 is not a Value
  5.    if (data == BLACK)
  6.    {
  7.       //do something
  8.    }
  9. }
  10.  
prevent values other than BLACK or RED to be placed in the data variable.

Unfortunately the above examples are C++ examples and they don't work in C because in C Value is not a type. Enter the enum typedef:
Expand|Select|Wrap|Line Numbers
  1. typedef enum x{BLACK, RED}  Value;
  2. int main()
  3. {
  4.    Value data = 20;   //ERROR 20 is not a Value
  5.    if (data == BLACK)
  6.    {
  7.       //do something
  8.    }
  9. }
  10.  
Now Value is a type. This code can be compiled both as C or as C++.

Does this help?
Nov 27 '07 #3
lspxy
12
typedef A B; just create an alias of A, that is B.
Jan 2 '12 #4
typedef int Unit;

It means 'Unit' now acts as integer datatype and has all its properties....
so instead of using int a,b; etc.....
we can use
Unit a,b;
where a and b act as integer variables.
Jan 3 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir...
6
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
0
by: zheng | last post by:
I create a OCX program with ATL project, and One parameter in the interface function is ENUM type, and I has defined the ENUM type in the interface, but compiled falid with MIDL2001, in the...
7
by: for.fun | last post by:
Hi everybody, I have the following problem : B class need A::MyEnum type and A class need B::MyEnum type. In both case, the class type is incomplete so it is obvious that the ::MyEnum can not...
7
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
3
by: =?Utf-8?B?Sm9uIEU=?= | last post by:
I have an interface class with maybe eight functions, defined in one workspace and am defining a class in a second workspace that derives from this interface. Unfortunately only 7 of the 8...
5
Plater
by: Plater | last post by:
I ran into this interesting little issue when make a settings window for a SerialPort. I have the following enum: public enum BaudRates : int { BR1200 = 1200, BR2400 = 2400, BR4800...
3
by: lingjun | last post by:
Hi, I am taking my first programing course in college... and I am completely lost on this assignment. I am not sure what is wrong with my current code. Any help will be appreciate it... thanks! ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.