何时在 C 中的链表中使用 NULL 以及何时使用 ''? [英] When to use NULL and when to use '' in linked list in C?

查看:22
本文介绍了何时在 C 中的链表中使用 NULL 以及何时使用 ''?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C 中了解到:null char == '' == NULL,我在下面写了一个循环来从 C 中的 char[] 的开头读取到结尾.

I learned that in C: null char == '' == NULL, and I wrote a loop below to read from the start to the end of a char[] in C.

// case #1
char buf[32];
while (buf[i] != NULL){
    //do something...
}  

然而,我的 gcc 编译器给了我一个警告:指针和整数之间的比较.有人提到我混淆了两个不同的概念:NULL 用于指针,而 '' 用于字符.所以为了摆脱警告,我应该使用 '' 因为我的循环测试了一个字符.

However, my gcc compiler gave me a warning: comparison between pointer and integer. Someone mentioned that I was confusing two separate concepts: NULL is for pointers, whereas '' is for characters. So to get rid of the warning, I should use '' since my loop tests a char.

现在我正在写一个链表,并测试一个头指针是否指向一个节点.由于它是结构体,因此使用 if (h1 == NULL) 是合理的,但显然当我使用 if (h1 == '') 时,编译器也会编译,即使该节点是一个结构,但不是一个字符.有人可以提供一些帮助,为什么在这种情况下可以同时使用 '' 和 NULL 而它们不能同时用于第一种情况?

Now I am writing a linked list, and testing if a head pointer points to a node or not. Since it's struct, it's reasonable to use if (h1 == NULL) but apparently the compiler also compiles when I use if (h1 == '') even though the node is a struct but not a char. Can someone give some help why both '' and NULL can be used in this case while they can't be both use on the first case?

// case #2
struct ListNode {
    int val;
    struct ListNode *next;
};

推荐答案

这是一个非常常见的混淆.空字符"(通常拼写为 NUL,由于历史原因,只有一个 L)是一个 字符,比较等于 0.空指针"是一个指针,比较等于零.区别仅在于类型——但这是一个重要的区别.编译器可以(但不是必须)反对您的程序,因为您正在将 character 值与空 指针 常量进行比较.

This is a very common confusion. A "null character" (often spelled NUL, with only one L, for historical reasons) is a character that compares equal to zero. A "null pointer" is a pointer that compares equal to zero. The difference is only the type -- but that is an important difference. The compiler is allowed, but not required, to object to your program, because you are comparing a character value to a null pointer constant.

C 有一个相当松散的类型系统,尤其是在涉及整数常量时,因此您可以逃避使用类型不正确的常量很多时候.当涉及到零时尤其如此:整数文字 0 可以在任何可以安全使用宏 NULL 或字符文字 ' 的地方使用'.事实上,在所有版本的 C 标准中,实现都特别允许使用 #define NULL 0.正因为如此,有一小部分上下文绝对必须将 (T*)0 用于某些具体类型 T,而不是 要么是裸 0NULL,例如将空指针传递给采用可变数量参数的函数时(例如 execl).

C has a fairly loose type system, especially when it comes to integer constants, so you can get away with using constants with a formally incorrect type a lot of the time. This is especially true when it comes to zero: the integer literal 0 can be used everywhere it is safe to use the macro NULL or the character literal ''. In fact, the implementation is specifically allowed to use #define NULL 0 in all editions of the C standard. Because of this, there are a small handful of contexts where one absolutely must use (T*)0 for some concrete type T, instead of either bare 0 or NULL, such as when passing null pointers to a function that takes a variable number of arguments (e.g. execl).

作为进一步的锦上添花,在 C 中,字符文字的类型为 int,而不是 char,并且 '' 是一个合法的空指针常量.(在 C++ 中,这些都不是真的.)

As further icing on the confusion cake, in C character literals have type int, not char, and '' is a legitimate null pointer constant. (In C++ neither of those things is true.)

IMNSHO 在 C 中的最佳实践是使用 '' 作为空 字符,而 0不是 NULL — 用于空指针.

Best practice in C, IMNSHO, is to use '' for the null character, and 0not NULL — for the null pointer.

这篇关于何时在 C 中的链表中使用 NULL 以及何时使用 ''?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆