fgets 和 NULL 之间的比较

标签 c

我刚刚在 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/

相关文章:

更正 sys_uname 的内联汇编代码

c - 使用 sscanf 时区分换行和错误

c - thrd_join 的返回值与其 res 参数的区别?

c - 任意浮点精度数到字符串

c - 如何垂直打印命令行参数?

c - c语言中 "literal" '\1'是什么定义?

c - __builtin_avr_delay_cycles 实现说明

c - 设置 size_t 的最后 3 位

分而治之递归算法的复杂性

c - 在 Clion 2017.3.4 中调试时的反汇编 View - 这意味着什么?