Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving a value to an enum in a struct in C

Tags:

c

I have following struct defined in a header file which I cannot edit (not allowed to):

struct Programma
{
enum {SERIE, FILM} programmaType;

char* titel;
char* zender;

time_t start;
time_t einde;

char* review;

union 
{
    Serie* serie;
    Film* film;
} typeData;
};

I've tried:

Programma asd;

asd.programmaType = SERIE; //Error: identifier 'SERIE' is undefined
asd.programmaType = 0; //Error: a value of type 'int'cannot be assigned to an entity of type 'enum PRogramma::<unnamed>

and in a function with a pointer to a Programma struct as parameter replacing the '.' by '->', which gave me the same errors.

I think I'm doing the same thing as suggested here (How to use enum within a struct in ANSI C?), but I can't stop getting random errors.

EDIT: For some reason in Visual Studio when I type asd. the usual dropdown box comes up, but there's an option for 'Typedata' as well as 'SERIE' and 'FILM'. Personally I think there's something wrong with the declaration of the struct, but it was given, and I'm not supposed to edit it.

EDIT EDIT: I'm creating a C++ project in Visual Studio 2008, but every source file is either .h or .c. This is what we've been taught as 'C', but I'm starting to wonder how true that actually is.

like image 300
Jelco Adamczyk Avatar asked Mar 04 '13 14:03

Jelco Adamczyk


3 Answers

It seems, by the errors messages, that you are actually not having a C program, but a C++ program. For that you have to use the scoping operator :: as in

asd.programmaType = Programma::SERIE;
like image 171
Some programmer dude Avatar answered Oct 05 '22 18:10

Some programmer dude


Assuming C, you're declaring a struct (without a typedef) incorrectly.

Try:

struct Programma asd;
like image 28
teppic Avatar answered Oct 05 '22 18:10

teppic


replace

Programma asd;

by

struct Programma asd;

then asd.programmaType = SERIE; should be accepted.

like image 44
Edouard Thiel Avatar answered Oct 05 '22 16:10

Edouard Thiel