2

I want to do something like this.

typedef struct Test{
 int value;
 struct Test* parent;
 struct Test** children;
}Test;

So I want a node that points to another parent structure. Then I want a dynamically allocated array that points to child nodes. My question is that I have no idea how this would work syntactically.

For example,

Test* first;
Test* second;
Test* third;
(*third).value = 1;
(*first).parent = second;
(*first).child[0] = third;
printf("%d\n",(*first).(*child[0]).value);

doesn't compile. I'm assuming I need to do something with malloc to allocate space for the array of pointers but I'm not sure. Also I'm not sure how I would access the "value" of the parent and child directories.

asked Apr 22, 2012 at 3:27

1 Answer 1

1

EDIT: I've added an ideone link to the end that implements all the concepts for you.

Sorry for the terseness of this answer, I am hoping it will show you how to do it properly.

Test* first = (Test *)malloc(sizeof(Test)); // malloc(sizeof(Test)) allocates enough memory to hold a Test struct
Test* second = (Test *)malloc(sizeof(Test));
first->value = 1; // -> is the proper way to dereference pointers in this situation (sorry wrong term? I am up late) but I suppose your style can work, it just gets a bit confusing IMO
first->*child = (Test *)malloc(intptr_t * number_of_children); // intptr_t will make sure you have the right size of a pointer, you could also use sizeof(Test *) instead. i.e. malloc(sizeof(Test *));
first->child[0] = second; // The array-style subscript is just more readable IMO
printf("%d\n",first->child[0]->value); // child[0]-> will handle the dereferencing in a nice way

But I'm going to show you a bit of a trick to make your life easier

typedef Test* test_array;
// ...later, in the struct...
test_array* child;
// ...later, in the malloc place...
first->child = (test_array *)malloc(sizeof(test_array *) * number_of_children);

Everything else stays the same, you just get much easier to understand syntax IMO. Helps deal with those tricky double stars.

EDIT: here's the link - http://ideone.com/TvSSB

answered Apr 22, 2012 at 3:33
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that helped. Although here it doesn't compile unless I cast the call to malloc. The error I'm getting in that case is "error: invalid conversion from ‘void*’ to ‘Test* {aka main()::Test*}’ [-fpermissive]"
Added casts so that malloc is happy
Also updated the malloc for first->child. Um, it's too late for me to determine if it should be first->child = (Test **) or first->*child = (Test *). I think the way I have it here is right. Double *'s are tricky.
@TomDavis finally, added a way to manage double *'s easier, I'm out for the night, vote me up and make me an answer if I've answered you :-P or ask more questions and I'll answer in the morning
@TomDavis grawr, double *'s are my bane, so I implemented it in an easy-to-read manner for you. check the link. really, bed now.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.