我刚刚在 David Griffiths 的书“Head First C”第 6 部分中看到了一段奇怪的代码:
island* start = NULL; island* i = NULL;
island* next = NULL;
char name[80];
for(; fgets(name, 79, stdin) != NULL; i = next) {
next = create(name);
if (start == NULL)
start = next;
if (i != NULL)
i->next = next;
}
这是部分源代码(如果可以这样命名的话):https://github.com/dogriffiths/HeadFirstC/blob/master/chapter5/Page%20218/code_lots_of_progs.c
第 250 行。
那么,fgets 和 NULL 之间的比较真的有效吗???
最佳答案
是的,它有效。 如果出现错误或没有输入下一个单词,则 fgets 返回 NULL。
关于fgets 和 NULL 之间的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25515006/