468,490 Members | 2,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

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 166980
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,207 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

Post your reply

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

Similar topics

reply views Thread by Patrick Guio | last post: by
6 posts views Thread by Fao | last post: by
9 posts views Thread by Xiangliang Meng | last post: by
7 posts views Thread by gretean | last post: by
3 posts views Thread by =?Utf-8?B?Sm9uIEU=?= | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.