?
Solved

how do you compare two typedef enum?

Posted on 2008-10-19
6
Medium Priority
?
739 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 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

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

801 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