473,770 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between "typedef enum" and "enum"

2 New Member
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 171464
mohammadazim
28 New Member
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 Recognized Expert Moderator Expert
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 New Member
typedef A B; just create an alias of A, that is B.
Jan 2 '12 #4
shilpa george
15 New Member
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
1975
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 must be able to be updated with more pairs later. Here is the solution I have developed so long All my enum-like int values are declared static const inside a namespace as well as a struct with a static map<int,string> to keep the pairs. The...
6
3735
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 "typedef" to contain only either +10 or -10 and the other type named Color declared by "enum" to contain only black, blue, purple, red, white, and yellow.
9
6219
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
1450
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 generated IDL file, I find the generated result is not the same with my think, and What can I do? The generated IDL is so: in the interface define file(header file): _interface IMyInterface:... { typedef enum _STATE
7
4718
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 be found in the class definition (the compiler complains) My problem is the MyEnum types have to be defined inside the class
7
12594
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. template <int x, int yclass M { public: template <int xRet, int yRet, int xR, int yR> M<xRet, yRetoperator *(const M<xR, yR&b) const { M<xRet,yReta; return a;
3
15754
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 functions in my derived class compile whilst just one of them refuses to be recognised, leading to an error message "...does not implement interface member...". Try as I might, I can't fix the error. I have cut and paste names so can rule out simple...
5
2718
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 = 4800, BR9600 = 9600,
3
13104
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! I keep on getting the follow error messages when I try to compile it. test.c:3: error: syntax error before numeric constant test.c: In function `main': test.c:18: error: `next_day' undeclared (first use in this function) test.c:18: error:...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.