Solved

# how do you compare two typedef enum?

Posted on 2008-10-19
Medium Priority
742 Views
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;
``````
0
Question by:kuntilanak

LVL 16

Expert Comment

ID: 22754808
0

LVL 45

Expert Comment

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)
{
...
}
``````
0

Author Comment

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;
``````
0

LVL 45

Expert Comment

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

rstaveley earned 2000 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

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

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soā¦
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month8 days, 3 hours left to enroll

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

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