【发布时间】:2013-08-27 14:45:58
【问题描述】:
只是试图让一些涉及指针、函数和递归的代码工作:
Node * root = malloc(sizeof(Node));
root->data = "1";
root->next = NULL;
root->child = NULL;
Node * G = NULL;
BuildGraph(root, &G);
printf("Root is: %d\n", root->data);
Print(G, ">>"); // Custom Print function
还有 BuildGraph:
void BuildGraph(Node * head, Node ** G) {
if (head->child == NULL) { // No child
printf("Head has no child! %d\n", head->data);
Push(head, &G);
Print(G, ">>");
return;
}
BuildGraph(head->child, &G);
return;
}
所以当我运行程序时,我的输出是这样的:
Head has no child! 1 // printf in BuildGraph
G: 1>> // Print(G, ">>") in BuildGraph
Root is: 1
G is empty! // Print(G, ">>") in main
有人知道 G 没有转入 main 的原因吗?
谢谢。
【问题讨论】:
标签: c function pointers recursion linked-list