• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Urgent question

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
RomanRega
Asked:
RomanRega
  • 2
1 Solution
 
sunnycoderCommented:
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
 
RomanRegaAuthor Commented:
Jeez the answer is that simple. :)

Im almost ashamed.

Thanks :)
0
 
Member_3480671Commented:
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
 
sunnycoderCommented:
> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now