Solved

how do you compare two typedef enum?

Posted on 2008-10-19
6
736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 69
Set custom font WPF 2 25
.NET Redemption/Outlook Examples 3 29
async questions 5 48
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

737 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