I came over this code which puzzled me. Isn't the enum supposed to
have an identifier?
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
Following this, I played around a bit, and came up with the program
below, which I had hoped would provoke the compiler into issuing a
warning about type mismatch (or something along those lines). Which
it didn't. (gcc -std=c99 -Wall -pedantic).
Care to comment?
With kind regards
Asbjørn Sæbø
#include <stdio.h>
typedef enum
{
M_A = 1,
M_B,
M_C,
M_D
} M_type;
typedef enum
{
N_A = 1,
N_B,
N_C,
N_D
} N_type;
void myfunc(M_type myvar)
{
printf("Inside myfunc, myvar is %d\n", myvar);
}
int main(void)
{
N_type a = N_B;
myfunc(a); /* Feed variable of N_type into function expecting M_type */
return 0;
} 10 9577
=?utf-8?b?QXNiasO4cm4 gU8OmYsO4?= <in*****@invali d.invalidwrote:
I came over this code which puzzled me. Isn't the enum supposed to
have an identifier?
Not necessarily.
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
This doesn't define a type, but it does define a number of constants.
This can on occasion be quite useful. It is, for example, easier to
maintain than a series of #defines.
Richard
Asbjørn Sæbø wrote:
I came over this code which puzzled me. Isn't the enum supposed to
have an identifier?
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
Does the FAQ entry <http://c-faq.com/struct/enumvsdefine.ht mlhelp?
Basically, I think it boils down to this - enums are for historical
reasons, rather weakly specified. So in effect, you are just creating
some integral constants here. Likewise in your example program, because
of the weak specification values from one enum can be used where
another is specified - enum types are "just" some integer type. ma**********@po box.com writes:
[...]
Does the FAQ entry <http://c-faq.com/struct/enumvsdefine.ht mlhelp?
Yes. Although this probably means that the practice I have seen of
typedef-ings enums does not help much when it comes to catching
errors.
Thanks, both to you and Richard Bos.
Asbjørn S.
"Asbjørn Sæbø" <in*****@invali d.invalidwrote in message
news:rg******** *****@ardbeg.no rdicsemi.no...
>
I came over this code which puzzled me. Isn't the enum supposed to
have an identifier?
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
Looks fine to me.
I don't know the standards -- I just have years of messing around with
compilers.
Based on years of messing around and randomly modifying code until it seems
to work most of the time ... an enum is generally interchangeable with an
integer, and the compiler won't enforce type. Passing an integer where an
enum is "expected" or passing an enumerated constant where an integer is
expected seems to generate no warnings or errors.
I don't know the standards, but the code excerpt above behaves about the
same as:
#define BT_CONNECTED (1)
#define BT_OPEN (2)
etc.
The only advantage to enums seems to be that they allow the compiler to
number the constants itself. In fact, in the example you gave, my code
would also traditionally include a symbol like "BT_MAX" at the end to test
for a corrupted or unexpected variable (and of course, the value of BT_MAX
will be modified if constants are added or removed earlier in the list).
Enums in C are treated oddly--the typing is very weak. The automatic
numbering of the constants is the only aspect of it I've found useful.
--
David T. Ashley (dt*@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page) http://gpl.e3ft.com (GPL Publications and Projects)
Asbjørn Sæbø <in*****@invali d.invalidwrites :
I came over this code which puzzled me. Isn't the enum supposed to
have an identifier?
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
Perfectly legal. The tag on an enum type declaration is optional.
The declaration creates a type (an anonymous one in this case),
but the constants BT_CONNECTED et al are actually of type int.
For the above declaration, the creation of the type (which can't be
used anyway) is a side effect; the purpose of the declaration is to
declare the constants. It's a cleaner alternatve to
#define BT_CONNECTED 1
#define BT_OPEN 2
...
Following this, I played around a bit, and came up with the program
below, which I had hoped would provoke the compiler into issuing a
warning about type mismatch (or something along those lines). Which
it didn't. (gcc -std=c99 -Wall -pedantic).
[...]
>
#include <stdio.h>
typedef enum
{
M_A = 1,
M_B,
M_C,
M_D
} M_type;
typedef enum
{
N_A = 1,
N_B,
N_C,
N_D
} N_type;
void myfunc(M_type myvar)
{
printf("Inside myfunc, myvar is %d\n", myvar);
}
int main(void)
{
N_type a = N_B;
myfunc(a); /* Feed variable of N_type into function expecting M_type */
return 0;
}
There's no problem with the code. Values of enum or integer types may
be implicitly converted back and forth.
It would have been nice, in some ways, if the rules for enumerated
types were more strict (as they are in some other languages), but
they're not.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Asbjørn Sæbø <in*****@invali d.invalidwrites :
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
Putting this in a comment is perverse. I would write this as:
BT_CONNECTED = TCP_ESTABLISHED ,
and then there's no need for the comment at all.
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
In article <87************ @blp.benpfaff.o rg>,
Ben Pfaff <bl*@cs.stanfor d.eduwrote:
>enum { BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
Putting this in a comment is perverse. I would write this as:
BT_CONNECTED = TCP_ESTABLISHED , and then there's no need for the comment at all.
Assuming that TCP_ESTABLISHED is defined at that point, which it might
well not be.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Asbjørn Sæbø wrote:
ma**********@po box.com writes:
>[...] Does the FAQ entry <http://c-faq.com/struct/enumvsdefine.ht mlhelp?
Yes. Although this probably means that the practice I have seen of
typedef-ings enums does not help much when it comes to catching
errors.
gcc (and all others out there too, probably) checks switch statements
for you:
typedef enum {
ONE,
TWO
} Something;
void func (Something s)
{
switch (s)
{
case ONE:
break;
} /* warning here - TWO not handled in switch */
}
And using enum type instead of int is a good documentation too,
which is a great help to *avoid* errors in the first place.
Regards,
Yevgen
Asbjørn Sæbø <in*****@invali d.invalidwrites :
ma**********@po box.com writes:
[...]
Does the FAQ entry <http://c-faq.com/struct/enumvsdefine.ht mlhelp?
Yes. Although this probably means that the practice I have seen of
typedef-ings enums does not help much when it comes to catching
errors.
As far as I can tell, typedef-ing an enum doesn't help *at all* when
it comes to catching errors. A typedef merely creates an alias for a
type; it doesn't create a new type.
Typedefs can be useful when creating an abstract data type (hiding the
representation details from clients), or when specifying a numeric
type that may vary from one implementation to another (such as
int32_t, which could be int, long, etc.) Most other uses of typedefs
are, in my opinion, not useful; you might as well refer to the
original type directly.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark A. Gibbs |
last post by:
i have been toying with the idea of making my enums smarter - ie, more
in line with the rest of the language. i haven't tested it yet, but what
i came up with is a template like this:
template <typename Enum>
class smart_enum
{
public:
typedef Enum enum_type;
|
by: narshe |
last post by:
I haven't come across an elegant way to do this yet, and would like to
know what other people have come up with.
There are two tables:
CREATE TABLE EmployeeStatus
(
pkId int not null primary key identity,
status varchar( 50 ) not null
)
|
by: MariuszC |
last post by:
Hello,
I have used some library where is defined enum like:
typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 };
In my code I have defined:
typedef enum Enum2 { ENUM2_1, ENUM2_2, ENUM2_3, ENUM2_4, ENUM2_5 };
My map function looks like:
|
by: Victor Eijkhout |
last post by:
Intel icc seems to think that enums are not ints:
#include <stdlib.h>
typedef enum { TRUE=1, FALSE=0 } Truth;
#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
|
by: aiooua |
last post by:
hello,
I had a doubt regarding the handling of enums in std::map.
Consider the following piece of code:
------
#include<iostream>
#include<map>
using namespace std;
typedef enum {
| |
by: borophyll |
last post by:
Hi all, what is the meaning of the second form of an enum declaration
enum identifier { enumerator-list }
enum identifier { enumerator-list , }
as described in the spec. I couldn't see anywhere in the spec that
describes what this second form (with the extra comma) means?
Regards,
B.
|
by: copx |
last post by:
C's enum type disappoints me a lot.
You cannot define which type of integer variable is used. This contradicts
C's low level spirit. "I want a number variable. I do not care about size or
execution speed." feels like typical ultra high level scripting language
design. The compiler could not optimize an enum if it wanted to, because you...
|
by: Daniel Gutson |
last post by:
Hi,
I just wanted to share another library for doing type-safe bitwise
operations in C++:
http://bitwise-enum.googlecode.com
I found it useful, so hopefully it'll be for somebody else as well.
BRgds,
Daniel.
|
by: Thomas Pornin |
last post by:
Hello,
with gcc-4.2.3, this does not compile:
typedef int foo;
int f(foo)
int foo;
{
return foo;
}
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| | |