【发布时间】:2018-04-07 22:14:09
【问题描述】:
#include <stdio.h>
int main(void)
{
struct findEntry
{
int value;
struct entry *next;
};
struct entry n1, n2, n3;
struct entry *list_pointer = &n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n2;
n3.value = 300;
n3.next = (stuct entry *) 0;
while (list_pointer != (struct entry *)0) {
printf("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
return 0;
}
我不明白(struct entry *) 0 的语法在这里是什么意思。在我正在阅读的书中,它说它是一个空指针。我尝试在网上查找,但我不知道该输入什么。我得到的“NULL 指针”的 Google 搜索结果与我的预期不同。
【问题讨论】:
-
n2.next = &n2;中还有一个拼写错误,这意味着我们永远无法到达n3。我会扔掉这个不好的例子并研究一些其他代码。 -
(struct entry *)0只是写NULL的不好方式。买一本新书。 -
请注意,您的代码定义了一个类型
struct findEntry,其中包含一个指向不完整类型struct entry的指针。您不能定义n1等,因为struct entry是不完整的类型。定义struct entry而不是struct findEntry所需的更改最少——或者您可以将struct entry更改为struct findEntry。此外,n3.next = (stuct entry *) 0;中有一个错字——struct中有一个r。请 — 发布可编译的代码! -
另外,
n2.next = &n2;行在列表中为您提供了一个循环,而n3未使用。代码编译后,您的打印循环将运行很长时间。 -
我认为代码作者使用的是C++编译器。