1

For school I have to write an agenda, it has to hold data about exams, tasks and lectures

I'm having trouble accessing an enum in my struct.

My struct looks as follows:

struct Item{

enum {TASK, EXAM, LECTURE} entryType;

char* name;
char* course;

time_t start;
time_t end;

union
{
    struct Task* task;
    struct Exam* exam;
    struct Lecture* lecture;
} typeData;
};

Now I have to set the type of the item using my enum. This struct is defined in Item.h. In Item.c which includes Item.h I use the following code:

struct Item* createItem(char* type){
struct Item* newItem;

newItem = (struct Item *) malloc (sizeof(struct Item));

if (strcmp(type, "Task") == 0)
{
    //newItem->entryType = newItem->TASK;
    newItem->typeData.task = createTask();
} else if (strcmp(type, "Exam") == 0)
{
    //newItem->entryType = newItem->EXAM;
    newItem->typeData.exam = createExam();
} else if (strcmp(type, "Lecture") == 0)
{
    //newItem->entryType = newItem->LECTURE;
    newItem->typeData.lecture = createLecture();
}

return newItem;
}

The commented code gives me the error (for TASK for example):

error C2039: 'TASK' : is not a member of 'Item'

10
  • TASK is not task. Commented Aug 3, 2013 at 14:46
  • What are you doing all the string comparison for? If you are using an enum, there is no reason for strcmp. Enum members work just like integers. Commented Aug 3, 2013 at 14:47
  • 1
    Also, don't cast the return value of malloc(). Commented Aug 3, 2013 at 14:47
  • The string comparison is because i get the type from user input, so I use strings to determine which item has to be created. The comparison part of the code works as I intend it to work, I just can't access newItem->TASK to set the type of the newly created item. Commented Aug 3, 2013 at 14:52
  • 1
    He probably thinks he's teaching C++. He's not, C and C++ are two different languages. You never cast in C, and you never use C-style casts in C++. Commented Aug 3, 2013 at 14:58

2 Answers 2

3

When you declare an enum, its contents essentially become compile time constants as if you had #defined them. In particular, if you have an enum { A, B, C } foo, you access the options by A, not foo->A as you seem to think.

Sign up to request clarification or add additional context in comments.

1 Comment

So how do you access enum Name1 { Value2 = 0x1;}? like the code I'm using needs to be able to pick the value name from 0x1, not Value2; like you would in a class
2

My first point is unnecessary, secondly change the parameter of createItem to an int, thirdly your using pointers in dataType so we really should see those functions, fourthly create a field of int in your struct called type.

   struct Item* createItem(int type){
   struct Item* newItem;

   newItem = malloc (sizeof(struct Item));    

  newItem->entryType = type;

   if (type == 0)
   {
     newItem->typeData.task = createTask();
   } else if (type == 1) 
   {
     newItem->typeData.exam = createExam();
   } else if (type == 2)
   {
     newItem->typeData.lecture = createLecture();
   }

 return newItem;
 }

5 Comments

Thanks for the tip about using ints for the type, I'll see to that. And the typeData functions don't give me any errors, they work so I don't see a reason to give the code for them. But the problem is in our assignment we already got the Item struct, and I copied it exactly. Is there no way to do it without making the enum global?
@crognar you can use int to access the value of enum. So any int and entryType are interchangeable.
@crognar the code I have up now doesn't require a global enum.
Thank you very much, this solved my problem. I guess you can't really access the different values which are defined inside the struct by using the names?
@crognar For simplicity yes, you can't just point to an enum value. If the enum was global your main would have knowledge of the enum value and send one along. Since it doesn't and you as the programmer know what is in the enum you can just pass along an int(assuming you did proper error checking) to the struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.