这是 gcc 4 上的错误消息:
test.c:6: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
test.c: In function ‘main’:
test.c:18: error: ‘struct binary_tree_node’ has no member named ‘left’
首先,您null
在NULL
C 中。其次,您不能为结构定义内的结构中的元素设置值。
所以,它看起来像这样:
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
main() {
struct binary_tree_node t;
t.left = NULL;
t.right = NULL;
t.value = 12;
struct binary_tree_node y;
y.left = NULL;
t.right = NULL;
y.value = 44;
t.left = &y;
}
或者,您可以创建一个函数来使 left 和 right 为 NULL,
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
void make_null(struct binary_tree_node *x) {
x->left = NULL;
x->right = NULL;
}
main() {
struct binary_tree_node t;
make_null(&t)
t.value = 12;
struct binary_tree_node y;
make_null(&y);
y.value = 44;
t.left = &y;
}