编辑:我在末尾添加了一个 ideone 链接,它为您实现了所有概念。
很抱歉这个答案很简洁,我希望它会告诉你如何正确地做到这一点。
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
但我要向你展示一个让你的生活更轻松的小窍门
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);
其他一切都保持不变,您只会更容易理解 IMO 的语法。帮助处理那些棘手的双星。
编辑:这是链接 - http://ideone.com/TvSSB