?
Solved

Urgent question

Posted on 2005-04-26
4
Medium Priority
?
261 Views
Last Modified: 2010-04-15
i defined the type of a node for a search tree like this:

typedef struct tSearchTree{
        char* s;
        struct tSearchTree* children[MAXCHILDREN];
};      

Can i have some kind of macro which makes me access to the children like below, without change the above typedef:
node->left; or node->right;

I tried this macros:
#define ->left ->children[0];
#define ->right ->children[1];

but naturally didn't work.

Any idea?
0
Comment
Question by:RomanRega
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 13865361
Hi RomanRega,

How about
#define  left children[0]
#define right children[1]

if you have variables named left or right within the functions, then I dont see a way to do what you are asking for and you will have to change the typedef

Cheers!
sunnycoder
0
 
LVL 3

Author Comment

by:RomanRega
ID: 13865374
Jeez the answer is that simple. :)

Im almost ashamed.

Thanks :)
0
 
LVL 1

Expert Comment

by:stuartdehaan
ID: 13865379
Perhaps you should remove the ->, but I'm not sure what you ask is possible with a macro.
You could define a struct like this:

typedef struct tSearchTree2{
        int left;
        int right;
}

typedef struct tSearchTree2{
        char* s;
        struct tSearchTree2* children[MAXCHILDREN];
};  

and then do a memcpy from the original, tSearchTree to tSearchTree2. Then you can access the copy, and once you're finished, copy it back.

Stuart
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13865403
> you will have to change the typedef
or the variable names ...

I would recommend changing typedef to left and right so that it is logocally clearer and easier to maintain. But from your question title, it seems that you do not have much time left to accomodate such changes, so #define and changing local variable names might be the way to go.

Cheers!
sunnycoder
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

840 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