我有這個代碼的一些問題。注意評論。爲什麼?爲什麼這三種方式在C代碼中有所不同?
struct node
{
struct node *left;
struct node *right;
int value;
};
static struct node root2;
int main()
{
struct node *root = (struct node *) malloc(sizeof(struct node));
assert(root->left == NULL); /* not failed. Why? */
assert(root->right == NULL); /* not failed. Why? */
assert(root2.left == NULL); /* not failed. Why? */
assert(root2.right == NULL); /* not failed. Why? */
struct node root1;
assert(root1.left == NULL); /* this failed. Why? */
assert(root1.right == NULL); /* this failed. Why? */
free(root);
return 0;
}
標準需要用'root2'所有'0'進行初始化,我從來沒有聽說過編譯器不會這麼做。 – 2011-02-28 10:56:24
適用於root和root1。 root2應該在符合實現(6.7.8/10)中初始化。 – AProgrammer 2011-02-28 10:56:57
感謝您的澄清,夥計們。將編輯。 – Dave 2011-02-28 11:18:00