如何在ANSI C中使用结构体中的enum ?

时间:2023-01-28 00:18:29

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

在主函数中必须使用以下代码,但是我不知道如何使用它。

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.

这个结构体用于具有前/项/下一个指针的动态链表中,但是我不知道如何设置enum。或者如何初始化它。

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”左边指定未定义的struct/union 'library' C2065: MOVIE: undeclare identifier

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

我仍然是ANSI C的新手,所以别开枪打我。

4 个解决方案

#1


13  

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

您使用的指针到处都是,所以您需要使用->来引用这些项。

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

ie。biglist - > someitem - > itemType =电影;

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分配任何内存!)

#2


4  

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

Or, if someitem is a pointer,

或者,如果某项是指针,

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

#3


0  

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

您可以以这种方式初始化enum ->someitem = MOVIE;但是编译器从0开始分配整数值。那么:biglist->someitem=MOVIE return 0或biglist->someitem=MUSIC return 1

check if it helps any good,

检查它是否有用,

#4


0  

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

#1


13  

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

您使用的指针到处都是,所以您需要使用->来引用这些项。

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

ie。biglist - > someitem - > itemType =电影;

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分配任何内存!)

#2


4  

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

Or, if someitem is a pointer,

或者,如果某项是指针,

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

#3


0  

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

您可以以这种方式初始化enum ->someitem = MOVIE;但是编译器从0开始分配整数值。那么:biglist->someitem=MOVIE return 0或biglist->someitem=MUSIC return 1

check if it helps any good,

检查它是否有用,

#4


0  

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