C语言 如何在 C 中使用枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15079954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use enum in C
提问by ShadyBears
Basically we have to implement a queue (linked list) for a restaurant waiting queue.
基本上我们必须为餐厅等待队列实现一个队列(链表)。
We get extra points for using enumbut I've never used it before. I'm wondering does this look right how I am using it? I have looked it up but haven't seen any examples using linked lists.
使用会加分,enum但我以前从未使用过。我想知道这看起来正确我如何使用它?我查过了,但没有看到任何使用链表的例子。
Here is the instructions for our structure:
这是我们结构的说明:
When writing your code, you MUST create a C struct for the nodes in the linked list of the wait list. These data items must include the following (and may include others if needed).
the name of the group
the integer variable specifying the size of the group (number of people in the group)
the in-restaurant status (extra points for using an enum!)
a pointer to the next node in the list
The restaurant status is walk-in or call-in (call ahead of time to put name on waiting list)
在编写代码时,您必须为等待列表的链表中的节点创建一个 C 结构。这些数据项必须包括以下内容(如果需要,还可以包括其他内容)。
组名
指定组大小的整数变量(组中的人数)
餐厅内状态(使用枚举的额外积分!)
指向列表中下一个节点的指针
餐厅状态是walk-in 或call-in(提前打电话把名字放在等候名单上)
Here's the structure of mine:
这是我的结构:
typedef struct restaurant
{
char name[30];
int groupSize;
enum status{call, wait};
struct restaurant *nextNode;
}list;
I'm asking because I get this warning when I compile:
我问是因为我在编译时收到这个警告:
lab6.c:11:28: warning: declaration does not declare anything [enabled by default]
lab6.c:11:28: warning: declaration does not declare anything [enabled by default]
采纳答案by paulsm4
Your struct typedef is basically saying "If I had a "status" field in my record, it could have the value "call" or the value "wait". The warning is basically saying "you never allocated a field".
您的 struct typedef 基本上是在说“如果我的记录中有一个“状态”字段,它的值可能是“调用”或值“等待”。警告基本上是说“您从未分配过字段”。
Possible change:
可能的变化:
enum status {CALL, WAIT};
typedef struct restaurant
{
char name[30];
int groupSize;
enum status my_status;
struct restaurant *nextNode;
}list;
Here's more info:
以下是更多信息:
回答by Jonathan Leffler
Your enummust either be declared outside the structure:
您enum必须在结构之外声明:
enum Status {call, wait};
typedef struct restaurant
{
char name[30];
int groupSize;
struct restaurant *nextNode;
} list;
or must declare a member of that type inside the structure:
或者必须在结构内声明该类型的成员:
typedef struct restaurant
{
char name[30];
int groupSize;
enum Status {call, wait} status;
struct restaurant *nextNode;
} list;
or both:
或两者:
enum Status {call, wait};
typedef struct restaurant
{
char name[30];
int groupSize;
enum Status status;
struct restaurant *nextNode;
} list;
You could create a typedef for the enum Statustoo. And since the tags (such as Statusin enum Status) are in a different namespace from structure members, you could actually use:
您也可以为它创建一个 typedef enum Status。由于标记(例如Statusin enum Status)与结构成员位于不同的命名空间中,因此您实际上可以使用:
enum status {call, wait} status;
and the compiler won't be confused but you might well be.
编译器不会感到困惑,但您可能会感到困惑。
Very often, people write enumeration constants in ALL_CAPS. This is partly a hangover from the days of using #define WAIT 0and #define CALL 1instead of enum Status { WAIT, CALL };.
很多时候,人们在 ALL_CAPS 中编写枚举常量。这在一定程度上是使用#define WAIT 0and#define CALL 1而不是enum Status { WAIT, CALL };.

