str==NULL 和 str[0]=='\0' 有什么区别在C语言中?
我想知道 str == NULL
和 str[0] == '\0'
之间的区别:
int convert_to_float(char *str, double *num)
{
if ((str == NULL) || (str[0] == '\0'))
return(-1);
*num = strtod(str, (char **)NULL);
return(0);
}
我在 Linux 上使用 gcc。
I want to know the difference between str == NULL
and str[0] == '\0'
:
int convert_to_float(char *str, double *num)
{
if ((str == NULL) || (str[0] == '\0'))
return(-1);
*num = strtod(str, (char **)NULL);
return(0);
}
I'm using gcc on Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
str==NULL
告诉你指针是否为NULL。str[0]=='\0'
告诉您字符串长度是否为零。在该代码中,测试:
用于捕获 NULL 或长度为零的情况。
请注意,短路在这里起着关键作用:测试的重点是确保
str
是长度至少为 1 的有效 c 字符串。str[0] == '\0'
仅当str
不为 NULL 时才有效。str == NULL
,以便在str
为NULL时尽早爆发。str==NULL
tells you whether the pointer is NULL.str[0]=='\0'
tells you if the string is of zero-length.In that code, the test:
is used to catch the case where it is either NULL or has zero-length.
Note that short-circuiting plays a key role here: The point of the test is to make sure that
str
is a valid c-string with length at least 1.str[0] == '\0'
will only work ifstr
is not NULL.str == NULL
is needed to break out early whenstr
is NULL.重要的是要记住,
str
并不是真正的“字符串”,而是指向char
(一个字符串的一部分)被存储。接下来,我们必须了解编译器如何看待所有这些项目。让我们看看它们的类型:
str
的类型为char *
(字面意思是“指向char
的指针”)NULL
是一个空指针常量(至少在我的系统上,它是((void*)0)
)'\0'
是一个字符常量(它实际上是 < 类型) code>int,但不用担心;它通常用在需要char
值的上下文中)看到
char *
和void *
中的*
了吗?这告诉编译器这些是指针类型(这是一种奇特的方式,表示这种类型的变量不保存值,它们只是指向它)。因此,当编译器看到char *str
时,它知道您可能会要求执行诸如*str
或str[0]
之类的操作(两者都做同样的事情)。稍后我们会再讨论这个问题。您会看到,当您在 C 程序中编写
str
时,编译器知道名为“str”的变量存储在内存位置中,例如 0x0001。它生成的代码转到 0x0001 并获取该值。这样,如果您执行类似的操作,则编译器将生成类似于以下内容的代码:
我确信您知道这一点。所以现在这行的意思应该很明显了:
由于
NULL
是一个空指针常量,所以该行测试str
是否是一个空指针(即,一个不存在的指针) t 指向任何东西)。因此,编译器通常会生成如下代码:
请记住,我们告诉编译器
str
实际上是一个指针类型。所以我们可以这样写:这使得编译器生成这样的:
所以如果 str 持有 0x0200,那么我们将从内存地址 0x0200 获取值。请注意,编译器并不真正关心字符串是否真的存储在那里。
(我假设您知道
str[0]
与*str
相同。它可以更轻松地解释发生了什么。)这个怎么样,然后?
因此,该行确实有效:
这使得编译器生成以下内容:
总结一下:
str == NULL
会告诉您指针str
是否指向 /strong> 什么也没有。*str == '\0'
告诉你指针str
是否指向空字符串(实际上,指向内存保持零的位置)。(根据定义,“字符串”是“以第一个空字符结尾并包含第一个空字符的连续字符序列”,因此如果字符串的第一个字符是
'\0'
,则字符串是空字符串。)It's important to remember that
str
isn't really a "a string", but rather a pointer to the memory location where achar
(a part of a string) is stored.Next, we have to understand how the compiler sees all of these items. Let's look at their types:
str
is of typechar *
(literally, "pointer to achar
")NULL
is a null pointer constant (at least on my system, it's((void*)0)
)'\0'
is a character constant (it's actually of typeint
, but don't worry about that; it's generally used in a context that requires achar
value)See the
*
inchar *
andvoid *
? That tells the compiler that these are pointer types (which is a fancy way of saying that variables of this type don't hold the value, they just point at it). So when the compiler seeschar *str
, it knows that you might ask to do something like*str
orstr[0]
(which both do the same thing). We'll get back to that, later.You see, when you write
str
in a C program, the compiler knows that a variable called "str" is stored in a memory location, for example 0x0001. The code it generates goes to 0x0001 and fetches the value. That way, if you do something likeThen the compiler will generate code that looks something like:
Which is something I'm sure you know. So now it should be obvious what this line says:
Since
NULL
is a null pointer constant, that line tests whetherstr
is a null pointer (i.e., a pointer that doesn't point to anything).So the compiler typically generates code like this:
Remember now, if you please, that we told the compiler that
str
is really a pointer type. So we're allowed to write this:And that makes the compiler generate this:
So if str held 0x0200, then we would get the value from the memory address 0x0200. Note that the compiler doesn't really care if a string is really stored there or not.
(I'm going to assume you know that
str[0]
is the same as*str
. It makes it easier to explain what's going on.)How about this, then?
So that line is really, in effect:
Which makes the compiler generate this:
To summarize:
str == NULL
tells you whether the pointerstr
is pointing at nothing.*str == '\0'
tells you whether the pointerstr
is pointing at a an empty string (actually, pointing at a memory location holding a zero).(A "string" is, by definition, "a contiguous sequence of characters terminated by and including the first null character", so if the very first character of a string is
'\0'
, then the string is an empty string.)本质上
str == NULL
确定str
是否为NULL
指针str[0] == '\0'
确定str
是否为 0 长度的 c 样式字符串当您组合它们时,您将检查它是否为 NULL 或空。这允许函数在方法开始时消除两种形式的空数据
Essentially
str == NULL
determines ifstr
is aNULL
pointerstr[0] == '\0'
determines ifstr
is a 0 length c style stringWhen you combine them you are checking if it NULL or empty. This allows the function to eliminate both forms of empty data at the start of the method
str == NULL
检查 str 是否为 NULL 指针(指向无处的指针)str[0] == '\0'
(如果不是 NULL 指针)检查第一个 str 元素有 0 值(没有字符的字符串仅以 0 结尾)str == NULL
checking str is NULL-pointer (pointer to nowhere)str[0] == '\0'
(if not NULL-pointer) checking first str element has 0-value (string without characters only 0-terminated)str==NULL
告诉您字符串是否为NULL
。*str=='\0'
告诉您字符串长度是否为零。注意:此答案是对 Mystical 的 15 秒答案的玩笑其中有
str=='\0'
。当然,前 3 或 4 分钟内所做的更改不会显示出来,他已修复它ಠ_ಠ。str==NULL
tells you whether the string isNULL
.*str=='\0'
tells you if the string is of zero-length.Note: This answer is a play on Mystical's 15 second answer which had
str=='\0'
. Of course changes made in the first 3 or 4 minutes aren't shown and he fixed it ಠ_ಠ.str == NULL 表示“str 指向内存地址零”(或系统上任何为 NULL 的地址)。通常这意味着根本没有字符串。
str[0] == '\0' 表示“str 的第一个字符是字符零”(它标记字符串的结尾)。这意味着有一个字符串,但它是空的。想象一个空杯子与根本没有杯子的情况;同样的想法。
在其他语言中,您可能会编写
str == null
与str == ""
。它们意味着两种不同的东西。了解 C 中的差异尤其重要,因为尝试使用 NULL 指针会使程序崩溃。str == NULL means "str points to memory address zero" (or whatever address is NULL on your system). Typically this means there is no string at all.
str[0] == '\0' means "the first character of str is character zero" (which marks the end of the string). This would mean there's a string, but it's empty. Think of an empty cup versus no cup at all; same idea.
In other languages you might write
str == null
vsstr == ""
. They mean two different things. It's especially important to understand the difference in C, since trying to use a NULL pointer will crash the program.str == NULL
表示该字符串没有字符串的引用,因为它的指针为空(表示字符串的地址为空)。str[0] == '\0'
--表示长度为0的字符串。如果此解释有任何问题或您仍有疑问,请告诉我。
str == NULL
it means that string having NO REFERENCE of string, because it's pointer is Null(means address of string is null).str[0] == '\0'
-- means string with 0 length.Please let me know if any thing is wrong in this explanation or you still have the doubt.
意味着str没有指向任何地址=指针为空。
并且
str[0] == '\0'
str 指向一个有效的地址,这一行检查第一个字符(即 str[0])是否为数字 0( ' 的 ascii 值) \0') 表示字符串结束。那么字符串是空的。 (str中没有字符:第一个是结束字符)means that str is not pointing to any adress = pointer is empty.
and
str[0] == '\0'
str is pointing to a valid adress and this line check if the first char (ie str[0]) is the digit 0 ( the ascii value of '\0') wich means the end of the string. then the string is empty. (there is no character in str : the first one is the end character)1->
str == NULL
判断str是否为NULL指针2->
str[0] == '\0'
确定 str 是否为 0 长度的 c 样式字符串,因此在此
if ((str == NULL) || (str[0] == '\0'))
OR 运算符的短路出现,因为它确保字符串不指向任何内容或空字符串。
1 ->
str == NULL
determines if str is a NULL pointer2 ->
str[0] == '\0'
determines if str is a 0 length c style stringso in this
if ((str == NULL) || (str[0] == '\0'))
shortcircuiting of OR operator come into picture as it ensures either string is not pointing to anything or to empty string..
C# 中的等价物是:
简单的意思是字符串是否为 NULL 或者空字符串。
C# equivalent of it is:
Simple meaning is whether the string is NULL or an empty string.