如何在ANSI C的结构体中使用枚举? [英] How to use enum within a struct in ANSI C?
问题描述
继code在主函数中使用,但我不知道如何使用它。
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;
};
};
这是结构与previous /项目/ next指针动态链表使用,但我不知道如何设置枚举。或如何初始化。
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.
我需要知道它会是什么样子的主要功能。
I need to know how it would look like in the main-function.
biglist.someitem = ???;
/* declaration I use */
struct Library* biglist;
更多code明白了什么我试着做。
more code to understand what Im trying to do.
struct Library{
struct SomeItem* someitem;
struct SomeItem* previousItem;
struct SomeItem* nextItem;
};
编译器错误:
C2037:的someitem左指定未定义的结构/联合图书馆
C2065:电影:未声明的标识符
compiler errors: C2037: left of 'someitem' specifies undefined struct/union 'library' C2065: MOVIE: undeclared identifier
我还在上ANSI C是菜鸟,所以不要拍我OK;)
Im still a rookie on ANSI C, so dont shoot me ok ;)
推荐答案
您正在使用指针无处不在,所以你需要使用 - >引用的项目
You're using pointers everywhere, so you need to use -> to reference the items.
IE浏览器。 biglist-> someitem-> ITEMTYPE = MOVIE;
下面code编译罚款用gcc -Wall -strict:
The below code compiles fine with 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;
}
(虽然这code不会,当然跑,因为我不分配为biglist任何内存和someitem!)
(Though this code won't run of course, as I'm not allocating any memory for biglist and someitem!)
这篇关于如何在ANSI C的结构体中使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!