C语言 如何在 ANSI C 的结构中使用枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:00:53  来源:igfitidea点击:

How to use enum within a struct in ANSI C?

cstructinitializationenumsdeclaration

提问by TheDudeAbides

Following code has to be used in the main-function, but I don't know how it is used.

在main-function中必须使用以下代码,但我不知道它是如何使用的。

struct SomeItem
{
    enum {MOVIE, MUSIC} itemType;
    union {
        struct Movie* movie;
        struct Music* music;
    };
};

this struct is used in a dynamic linked list with previous/item/next pointer, but I don't know how you can set the enum. Or how to initialize it.

这个结构体用在带有previous/item/next 指针的动态链表中,但我不知道如何设置枚举。或者如何初始化它。

I need to know how it would look like in the main-function.

我需要知道它在主函数中的样子。

biglist.someitem = ???;
/* declaration I use */
struct Library* biglist;

more code to understand what Im trying to do.

更多代码来理解我想要做什么。

struct Library{
struct SomeItem* someitem;
struct SomeItem* previousItem;
struct SomeItem* nextItem;
};

compiler errors: C2037: left of 'someitem' specifies undefined struct/union 'library' C2065: MOVIE: undeclared identifier

编译器错误:C2037:“someitem”的左侧指定了未定义的结构/联合“库”C2065:电影:未声明的标识符

Im still a rookie on ANSI C, so dont shoot me ok ;)

我仍然是 ANSI C 的新手,所以不要对我开枪 ;)

采纳答案by JosephH

You're using pointers everywhere, so you need to use -> to reference the items.

您到处都在使用指针,因此您需要使用 -> 来引用项目。

ie. biglist->someitem->itemType = MOVIE;

IE。 biglist->someitem->itemType = MOVIE;

The below code compiles fine with gcc -Wall -strict:

下面的代码用 gcc -Wall -strict 编译得很好:

struct SomeItem
{
    enum {MOVIE, MUSIC} itemType;
    union {
        struct Movie* movie;
        struct Music* music;
    } item;
};

struct Library{
   struct SomeItem* someitem;
   struct SomeItem* previousItem;
   struct SomeItem* nextItem;
};

int main(void)
{
   struct Library* biglist;

   biglist->someitem->itemType = MOVIE;

   return 0;
}

(Though this code won't run of course, as I'm not allocating any memory for biglist and someitem!)

(虽然这段代码当然不会运行,因为我没有为 biglist 和 someitem 分配任何内存!)

回答by Tyler McHenry

biglist.someitem.itemType = MOVIE; /* or = MUSIC */

Or, if someitemis a pointer,

或者,如果someitem是一个指针,

biglist.someitem->itemType = MOVIE; /* or = MUSIC */

回答by Siddique

struct SomeItem 
{ 
    enum {MOVIE, MUSIC} itemType; 
    union { 
        struct Movie* movie; 
        struct Music* music; 
    } item;
    struct SomeItem *next;
}; 

回答by deepseefan

You may initialize the enum in such a way biglist->someitem = MOVIE; but the compiler assigns integer values starting from 0. So: biglist->someitem=MOVIE returns 0 or biglist->someitem=MUSIC return 1

你可以这样初始化枚举 biglist->someitem = MOVIE; 但是编译器从 0 开始分配整数值。所以: biglist->someitem=MOVIE 返回 0 或 biglist->someitem=MUSIC 返回 1

check if it helps any good,

检查它是否有帮助,