difference between "typedef enum" and "enum"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msukumarbabu
    New Member
    • Nov 2007
    • 2

    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
  • mohammadazim
    New Member
    • Aug 2007
    • 28

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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.

      [code=c]
      typedef int BYTE;
      [/code]

      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.

      [code=c]
      typedef char* String;
      [/code]

      This typedef allows you to use String as a type rather than use char*:
      [code=c]
      String Label = "Made in China";
      [/code]

      In C for structs you can:
      [code=c]
      struct x
      {
      int month;
      int day;
      int year;
      }Date;

      typedef Date* DatePtr;
      [/code]
      This makes Date a keyword and pDate a keyword. Now you could:
      [code=c]
      Date dt;
      Dateptr pd = &dt;
      [/code]

      In C without the Date keyword you have to code:
      [code=c]
      struct Date dt;
      Dateptr pd = &dt;
      [/code]
      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:
      [code=c]
      typedef int (*Compare)(cons t char*, const char*);
      [/code]

      This makes Compare the type for the function pointer:
      [code=c]
      Compare cmp = strcmp;
      [/code]

      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:
      [code=cpp]
      enum Value {BLACK, RED};
      int main()
      {
      int data = 20;
      if (data == BLACK)
      {
      //do something
      }
      }
      [/code]
      By using the enum as a type you can:
      [code=c]
      enum Value {BLACK, RED};
      int main()
      {
      Value data = 20; //ERROR 20 is not a Value
      if (data == BLACK)
      {
      //do something
      }
      }
      [/code]
      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:
      [code=c]
      typedef enum x{BLACK, RED} Value;
      int main()
      {
      Value data = 20; //ERROR 20 is not a Value
      if (data == BLACK)
      {
      //do something
      }
      }
      [/code]

      Now Value is a type. This code can be compiled both as C or as C++.

      Does this help?

      Comment

      • lspxy
        New Member
        • Nov 2011
        • 12

        #4
        typedef A B; just create an alias of A, that is B.

        Comment

        • shilpa george
          New Member
          • Oct 2011
          • 15

          #5
          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.

          Comment

          Working...