什么时候在C中的链表中使用NULL以及何时使用'\ 0'? [英] When to use NULL and when to use '\0' in linked list in C?
问题描述
我在C:null char == '\0' == NULL
中了解到这一点,并且在下面编写了一个循环,以从C中的char []的开头到结尾进行读取.
I learned that in C: null char == '\0' == 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用于指针,而'\ 0'用于字符.因此,要摆脱警告,我应该使用"\ 0",因为我的循环测试了一个char.
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 '\0' is for characters. So to get rid of the warning, I should use '\0' since my loop tests a char.
现在,我正在编写一个链表,并测试头指针是否指向节点.由于它是结构体,因此使用if (h1 == NULL)
是合理的,但是显然,即使节点是结构体而不是char,编译器也会在我使用if (h1 == '\0')
时进行编译.有人可以提供一些帮助,为什么在这种情况下不能同时使用'\ 0'和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 == '\0')
even though the node is a struct but not a char. Can someone give some help why both '\0' 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)是一个字符,它等于零. 空指针"是一个指针,它等于零.区别仅在于类型-但这是重要的区别.允许(但不是必需)编译器反对您的程序,因为您正在将 character 值与空的 pointer 常量进行比较.
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
或字符文字'\0'
的地方使用.实际上,该实现特别允许在C标准的所有版本中使用#define NULL 0
.因此,在少数情况下,对于某些具体类型T,绝对必须使用(T*)0
,而不是裸0
或NULL
的代替,例如在传递空指针时到带有可变数量参数(例如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 '\0'
. 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
,并且'\0'
是合法的 null指针常量. (在C ++中,这些都不是正确的.)
As further icing on the confusion cake, in C character literals have type int
, not char
, and '\0'
is a legitimate null pointer constant. (In C++ neither of those things is true.)
IMNSHO中C的最佳做法是将'\0'
用作空的字符,而将0
— not NULL
—用作空的指针.
Best practice in C, IMNSHO, is to use '\0'
for the null character, and 0
— not NULL
— for the null pointer.
这篇关于什么时候在C中的链表中使用NULL以及何时使用'\ 0'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!