472,779 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 170815
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.