views:

204

answers:

4

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.

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

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

+2  A: 
biglist.someitem.itemType = MOVIE; /* or = MUSIC */

Or, if someitem is a pointer,

biglist.someitem->itemType = MOVIE; /* or = MUSIC */
Tyler McHenry
Thanks Tyler, I have tried to do that, but the program still won't compile. I feel I have to give additional code..
TheDudeAbides
You should post the code that you are using to attempt to set the value (including declarations for all the variables involved), and the exact error message produced by the compiler. And edit this stuff into your question; don't reply with it as a comment.
Tyler McHenry
Did that just now.
TheDudeAbides
A: 

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

Siddique
A: 

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

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

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

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

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!)

JosephH
Thanks! both of you!
TheDudeAbides
A: 

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

check if it helps any good,

deepseefan