Solved

how do you compare two typedef enum?

Posted on 2008-10-19
6
731 Views
Last Modified: 2010-08-05
say I have the following code


and I have two different treenode struct one is called x and the other is y, how can I compare both treenode if they have the same type? I tried using x->type == y->type , but it doesn't work.. can someone please help me out.
typedef enum {x,y,z} Type;   
 

typedef struct treeNode   

   {

     .................

     Type type;    

     ................

   } TreeNode;

Open in new window

0
Comment
Question by:kuntilanak
6 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 22754808
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22754981
>but it doesn't work..
Can you show us the exact code and what problems you are facing?
From your code snippet it seems that you have x as enum value as well as pointer name. Is that really the case?
The == operator you used is indeed the C way of comparing types. e.g.
typedef enum {x,y,z} Type;   

 

typedef struct treeNode   

   {

     .................

     Type type;    

     ................

   } TreeNode;
 

TreeNode * i, *j;

...
 

if (i->type == j->type)

{

   ...

}

Open in new window

0
 

Author Comment

by:kuntilanak
ID: 22755155
well that's the actual code that I have:

I only use x,y,z for simplification of an example, I guess it won't matter though.. If you would like it in more detail:



typedef enum {Void,Integer,Boolean,String,Char, Null} Type;   
 

typedef struct treeNode   

   { 

     Type type;    
 

   } TreeNode; 

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22755161
Then comparing type fields is the way to go

TreeNode * i, *j;
...
 
if (i->type == j->type)
{
   ...
}

What were the issues you faced?
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 22755248
If this was C++, you'd have a common class that these were derived from, which returns the type and use inheritance. C++ is great at this sort of things, because you might even find that you could dispense with type altogether and use virtual functions to make an x node do x things and a y node do y things through something called polymorphism, which is easier than it sounds.

However, since this appears to be C, consider having type as the first element in your structure and treating your tree as a tree of commonNodes and casting those nodes as xNodes, yNodes and zNodes as applicable, when you determine the type.

e.g.

typedef enum {x,y,z} Type;  
 
typedef struct commonNode  /* Your tree is supposedly made up of these. This has the minimum information that all nodes carry.  */
   {
     Type type;  /* This is the only element */  
   } TreeNode;

typedef struct xNode /* Actually some of the elements are these */
   {
     Type type;  /* This is the first element */
     int xStuff;
   }

typedef struct yNode /* Actually some of the elements are these */
   {
     Type type;  /* This is the first element */
     char yStuff[64];
   }

/* Here you have to deal with a node in your tree. You don't know what the node really is, but you know that it has a Type at offset 0 and can therefore deal with it as a commonNode */
void foo(commonNode *pNode)
{
   /* If this is actually an xNode, cast it as such */
   if (pNode->type == x) {
      xNode* pXNode = (xNode*)pNode;
      /* .... */
   }
}

In practice you may want to put pointers into commonNode to allow it to point to other nodes in the tree. You might consider making an entire commonNode struct the first element of an xNode, yNode and zNode.

Another C-ish approach for this is using a union. Rather than having separate structs for commonNode, xNode, yNode and zNode you make one struct for all and union the xNode, yNode and zNode elements so that they overlay the same space in memory.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 22774309
> typedef enum {Void,Integer,Boolean,String,Char, Null} Type;

Incidentally, you might want to look at VARIANT data type you get in COM - see http://msdn.microsoft.com/en-us/library/ms221627.aspx - you'll see that they went the union route.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now