C struct and pointer mess

hi!
I think my code is getting a little bit messy.

I started building my ownlittle tree with soem structures:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct{//branches
 char name;
 void *where;
}branch;

typedef struct{//node
 void *back;
 branch *branches;
 int branchsize;
 int number;
}node;

[...]
int pos;
node* thenode;
[...]
if(((*((node*)((((*thenode).branches)[pos]).where))).branches)!=NULL){
[...]


(I ommited the non essential parts)

that if looks kinda NOT GOOD to me.
any advice on cleaning that mess a bit?
(I'm not sure about the operator prioritys so i used () everywhere, wich messes that up even more. at least help me remove some unnecessary brakets.

yours nafnaf
Last edited on
Are you trying to code a binary tree?
nope I'm coding a wierd tree wich can have as many branches per node as I need.
and as most will propably have 1 or 2, but it can get up to 30, i decided to put the branches into a branch* and malloc them whenever a new branche is added to a node.
Have you considered this:

1
2
3
4
5
6
7
#include <vector>

struct Node
{
    int value;
    std::vector<Node*> children;
};

Edit: you can create the following tree the following way:

http://oi42.tinypic.com/2qtv68h.jpg

1
2
3
4
5
6
7
8
Node* root = new Node(5);

root->children.push_back(new Node(4));
root->children.push_back(new Node(6));

root->children[0]->children.push_back(1);
root->children[0]->children.push_back(2);
root->children[0]->children.push_back(3);
Last edited on
Thanks for the help, but you seem to hev musunderstood my problem:

my tree works fine.
and only that monsterous if up there is what is bugging me.

and sorry for using C.
not C++ ;)

(i use headers stdio.h and stdlib.h)

the program I'm making is something only i will ever see the code, but i want to train not messing up my code too much.
Ok I managed to condense the if to the following:

1
2
3
if(((*((node*)((((*thenode).branches)[pos]).where))).branches)!=NULL){}

if((((node*)thenode->branches[pos].where)->branches)!=NULL){}


still feels a bit long to me^^

but I will keep it as it is.


Topic archived. No new replies allowed.