没有标识符,枚举和typedef的枚举 [英] Enums without identifier, enums and typedef

查看:127
本文介绍了没有标识符,枚举和typedef的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到了这个困扰我的代码。恩赐不应该

有一个标识符吗?


enum {

BT_CONNECTED = 1,/ *等于TCP_ESTABLISHED让网络代码开心* /

BT_OPEN,

BT_BOUND,

BT_LISTEN,

BT_CONNECT,

BT_CONNECT2,

BT_CONFIG,

BT_DISCONN,

BT_CLOSED

};

接下来,我玩了一下,然后想出了下面的程序

,我希望这会激发编译器发出一个

警告关于类型不匹配(或沿着这些线的东西)。哪个

它没有。 (gcc -std = c99 -Wall -pedantic)。


关注评论?


亲切的问候

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 \ nn",myvar);

}

int main(void)

{

N_type a = N_B;


myfunc(a); / *将N_type的变量输入函数期望M_type * /

返回0;

}

解决方案

=?utf-8?b?QXNiasO4cm4gU8OmYsO4?=< in ***** @ invalid.invalidwrote:


我遇到这个代码很困惑我。恩赐不应该

有标识符吗?



不一定。


enum {

BT_CONNECTED = 1,/ *等于TCP_ESTABLISHED以使网络代码高兴* /

BT_OPEN,

BT_BOUND,

BT_LISTEN,

BT_CONNECT ,

BT_CONNECT2,

BT_CONFIG,

BT_DISCONN,

BT_CLOSED

};



这并没有定义类型,但确实定义了许多常量。

这有时候非常有用。例如,比一系列#defines更容易维持



Richard


< blockquote>

Asbj?rn S?b?写道:


我遇到了这个困惑我的代码。恩赐不应该

有一个标识符吗?


enum {

BT_CONNECTED = 1,/ *等于TCP_ESTABLISHED让网络代码开心* /

BT_OPEN,

BT_BOUND,

BT_LISTEN,

BT_CONNECT,

BT_CONNECT2,

BT_CONFIG,

BT_DISCONN,

BT_CLOSED

};



FAQ条目< http://c-faq.com/struct/enumvsdefine.htmlhelp?


基本上,我认为可以归结为这一点 - 枚举是出于历史原因的b
,相当弱的指定。所以实际上,你只是在这里创建了一些积分常数

。同样在你的示例程序中,因为来自一个枚举的弱规范值的
可以用于

另一个指定 - 枚举类型是只是某种整数类型。


ma ** ********@pobox.com 写道:


[...]

请问FAQ条目< http://c-faq.com/struct/enumvsdefine.htmlhelp?



是的。虽然这可能意味着我所看到的

typedef-ings枚举的练习在捕捉

错误方面没有多大帮助。


非常感谢你和Richard Bos。


Asbj ?? sn



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;
}

解决方案

=?utf-8?b?QXNiasO4cm4gU8OmYsO4?= <in*****@invalid.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.htmlhelp?

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**********@pobox.com writes:

[...]
Does the FAQ entry <http://c-faq.com/struct/enumvsdefine.htmlhelp?

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.


这篇关于没有标识符,枚举和typedef的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆