0
我不明白爲什麼這個litle代碼不起作用!我從C struct and malloc problem (C)得到它(選定的答案),我想知道爲什麼它不適合我。malloc結構C
有什麼想法嗎?
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
/* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
tree* atree = malloc(sizeof(tree)); /* different names for variables */
node* anode = malloc(sizeof(node));
atree->nodes[0] = anode; // <-------- SEG FAULT HERE !
return atree;
}
int main() {
tree* mytree = initTree();
return 0;
}
atree-> nodes'指向未分配。 – BLUEPIXY 2014-10-18 23:38:16
right thx尋求你的幫助 – zeomega 2014-10-18 23:42:15