【发布时间】:2016-05-21 17:20:11
【问题描述】:
我正在阅读 C 语言中的 structs,但我并不完全理解 struct 的初始化方式。
请考虑以下代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
int main (void)
{
struct tnode *root;
struct tnode tn1;
root = &tn1;
printf("%d\n", root->count);
if (root->word == NULL)
printf("word is NULL\n");
else
printf("word is not NULL\n");
if (root->right == NULL)
printf("rightis NULL\n");
else
printf("right is not NULL\n");
}
输出:
0
word is NULL
right is not NULL
我不明白为什么root->right 没有初始化为NULL。有人可以请教一下吗?
【问题讨论】:
-
试试
static struct tnode *root;看看结果。
标签: c struct initialization