void print_node(unsigned i,unsigned j,direction dir,const node * np); 谢谢 Alex PS:我在OpenBSD 3.7上使用gcc 3.3.5。Hi,I have a UNIX-server written in C, which exchanges messageswith Java clients. In the message the 1 byte is its length,the 2nd a player number and the 3rd byte is an event id:/* 0. byte: Total message length in bytes (inlcuding this byte) *//* 1. byte: Player number (0, 1 or 2) *//* 2. byte: Event id (10 - 22) *//* 3.-255.: Argument (252 bytes or 126 big-endian UTF-16 chars) */typedef unsigned char byte;typedef struct {byte len,num,id,arg[MAXARG];} msg;For the event ids I currently use #defines:#define PREF_NAME 10#define PREF_PASS 11#define PREF_CHAT 12#define PREF_CARD 13But I would actually prefer to use enums instead, something like:enum event_id {PREF_NAME = 10,PREF_PASS,PREF_CHAT,PREF_CARD}However enums are integers, aren''t they? CouldI somehow specify that my enums are 8 bit long only?One more question: how do you use typedef''s with enum''s?I''ve tried to change this code:enum direction {FROM,TO};void print_node(unsigned i, unsigned j, enum direction dir, const node*np);To the following, but get strange unrelated error messages(I suspect that I declare a variable "direction" there instead):typedef enum {FROM,TO} direction;void print_node(unsigned i, unsigned j, direction dir, const node *np);Thank youAlexPS: I''m using gcc 3.3.5 on OpenBSD 3.7.推荐答案 #enum event_id { #PREF_NAME = 10, #PREF_PASS, #PREF_CHAT, #PREF_CARD #} # #但枚举是整数,不是'他们呢?可以 #我以某种方式指定我的枚举只有8位长? 使用byte而不是enum event_id。 字节变量= PREF_NAME; 这是允许的,假设PREF_NAME在值范围内 字节可以容纳,则不会丢失价值。 #还有一个问题:你如何在enum'中使用typedef? #我试图更改此代码: # #enum direction { #FROM, #TO #} ; # #void print_node(unsigned i,unsigned j,enum direction dir,const node #* np); # #以下,但得到奇怪的无关错误消息 #(我怀疑我在那里声明了一个变量方向): # #typedef enum { #FROM, #TO #}方向; 如果你想更好的合作控制方向分配 对象,你可以使用 typedef字节方向; 代替。 没有看到错误信息,很难猜到。 typedef 是否继续引用类型名称。 - SM Ryan http://www.rawbw.com/~wyrmwif/ 你讨厌人们。 但我喜欢聚会。不具讽刺意味。# enum event_id {# PREF_NAME = 10,# PREF_PASS,# PREF_CHAT,# PREF_CARD# }## However enums are integers, aren''t they? Could# I somehow specify that my enums are 8 bit long only?Use byte instead of enum event_id.byte variable = PREF_NAME;This is allowed and assuming PREF_NAME is in the range of valuesbyte can hold, there will be no loss of value.# One more question: how do you use typedef''s with enum''s?# I''ve tried to change this code:## enum direction {# FROM,# TO# };## void print_node(unsigned i, unsigned j, enum direction dir, const node# *np);## To the following, but get strange unrelated error messages# (I suspect that I declare a variable "direction" there instead):## typedef enum {# FROM,# TO# } direction;If you want to better control over the allocation of directionobjects, you can usetypedef byte direction;instead.Without seeing the error message, it''s hard to guess. Does the typedefproceed any reference to the type name.--SM Ryan http://www.rawbw.com/~wyrmwif/You hate people.But I love gatherings. Isn''t it ironic. Alexander Farber写道:Alexander Farber wrote: / * 0.字节:总消息长度(以字节为单位)(包含此内容) byte)* / / * 1.字节:播放器编号(0,1或2)* / / * 2.字节:事件ID(10 - 22)* / / * 3.-255。:参数(252字节或126个大端UTF-16字符)* / typedef unsigned char byte; typedef struct { byte len, num, id, arg [MAXARG]; } msg; 对于事件ID我目前使用#defines: #define PREF_NAME 10 #define PREF_PASS 11 #define PREF_CHAT 12 #define PREF_CARD 13 但我其实更愿意使用枚举,比如: enum event_id { PREF_NAME = 10, PREF_PASS, PREF_CHAT, PREF_CARD } 然而,枚举是整数,不是吗? C99草案中可能对你有帮助的行情: 枚举: 限制因素: .... "枚举器列表中的标识符是 声明为类型为int的常量和 可能出现在允许的任何地方...... 和脚注: 实施可能会延迟选择哪个 整数类型,直到所有枚举常量都显示为。 可以我以某种方式指定我的枚举只有8位长? 你为什么要这样? 还有一个问题:你如何在enum'中使用typedef'?我'我试图改变这段代码: enum direction { typedef enum direction {FROM, TO }; }方向; 应该有效。 void print_node(unsigned i,unsigned j,enum direction dir,const node * np); 对于以下内容,但得到奇怪的无关错误消息(我怀疑我在那里声明了一个变量方向): 可能的,使用你的文本编辑器的搜索选项到剑柄;-) 但枚举有自己的命名空间,甚至包括: typedef enum _myBool {FALSEY,TRUEY} _myBool; void demo(_myBool f) { int _myBool; / *以下是摆脱未使用的变量警告* / f =!f; _myBool = 1; } int main() { } 编译(使用gcc -std = c99 -W -Wall -pedantic),没有任何警告。 如果你发布了错误信息,它会很方便。 typedef enum { FROM, T O }方向; 也应该有效! void print_node(unsigned i,unsigned j,direction dir,const node * np); 谢谢 Alex PS:我正在使用gcc 3.3 .5在OpenBSD 3.7上。 Hi, I have a UNIX-server written in C, which exchanges messages with Java clients. In the message the 1 byte is its length, the 2nd a player number and the 3rd byte is an event id: /* 0. byte: Total message length in bytes (inlcuding this byte) */ /* 1. byte: Player number (0, 1 or 2) */ /* 2. byte: Event id (10 - 22) */ /* 3.-255.: Argument (252 bytes or 126 big-endian UTF-16 chars) */ typedef unsigned char byte; typedef struct { byte len, num, id, arg[MAXARG]; } msg; For the event ids I currently use #defines: #define PREF_NAME 10 #define PREF_PASS 11 #define PREF_CHAT 12 #define PREF_CARD 13 But I would actually prefer to use enums instead, something like: enum event_id { PREF_NAME = 10, PREF_PASS, PREF_CHAT, PREF_CARD } However enums are integers, aren''t they?Quotes from the C99 draft that might help you:Enum:Constraints:...."The identifiers in an enumerator list aredeclared as constants that have type int andmay appear wherever such are permitted..."and a footnote:"An implementation may delay the choice of whichinteger type until all enumeration constants havebeen seen."Could I somehow specify that my enums are 8 bit long only?Why would you want that? One more question: how do you use typedef''s with enum''s? I''ve tried to change this code: enum direction {typedef enum direction { FROM, TO };} direction;should work. void print_node(unsigned i, unsigned j, enum direction dir, const node *np); To the following, but get strange unrelated error messages (I suspect that I declare a variable "direction" there instead):Possible, use your text editor''s search option to the hilt ;-)But enums have their own namespace, and even the following:typedef enum _myBool { FALSEY, TRUEY } _myBool;void demo( _myBool f ){int _myBool;/* following are to get rid of unused variable warning */f = !f;_myBool = 1;}int main(){}compiles (with gcc -std=c99 -W -Wall -pedantic) without any warnings.Had you posted the error message(s), it would have been handy. typedef enum { FROM, TO } direction;Should work as well! void print_node(unsigned i, unsigned j, direction dir, const node *np); Thank you Alex PS: I''m using gcc 3.3.5 on OpenBSD 3.7. HTHHTH Alexander Farber写道:Alexander Farber wrote:我有一个用C语言编写的UNIX服务器,它与Java客户端交换消息。在消息中,1字节是其长度,第2个是播放器编号,第3个字节是事件ID: / * 0.字节:总消息长度(以字节为单位)(包含此字节)* / / * 1.字节:播放器编号(0,1或2)* / / * 2.字节:事件ID(10 - 22)* / / * 3.-255 。:参数(252字节或126个big-endian UTF-16字符)* / typedef unsigned char byte; typedef struct { byte len, num, id, arg [MAXARG]; } msg; I have a UNIX-server written in C, which exchanges messages with Java clients. In the message the 1 byte is its length, the 2nd a player number and the 3rd byte is an event id: /* 0. byte: Total message length in bytes (inlcuding this byte) */ /* 1. byte: Player number (0, 1 or 2) */ /* 2. byte: Event id (10 - 22) */ /* 3.-255.: Argument (252 bytes or 126 big-endian UTF-16 chars) */ typedef unsigned char byte; typedef struct { byte len, num, id, arg[MAXARG]; } msg; 你怎么知道字段之间没有填充?我建议 想一想。 - Anton PetrusevichHow do you know that there''s no padding between fields? I would suggest tothink about it.--Anton Petrusevich 这篇关于确保枚举符合unsigned char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 07:14