str==NULL 和 str[0]=='\0' 有什么区别在C语言中?

发布于 2024-12-18 11:51:33 字数 292 浏览 2 评论 0原文

我想知道 str == NULLstr[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

谈下烟灰 2024-12-25 11:51:33

str==NULL 告诉你指针是否为NULL。

str[0]=='\0' 告诉您字符串长度是否为零。

在该代码中,测试:

if ((str == 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:

if ((str == NULL) || (str[0] == '\0'))

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.

  • The second test str[0] == '\0' will only work if str is not NULL.
  • Therefore, the first test str == NULL is needed to break out early when str is NULL.
灼痛 2024-12-25 11:51:33

重要的是要记住,str 并不是真正的“字符串”,而是指向 char(一个字符串的一部分)被存储。

接下来,我们必须了解编译器如何看待所有这些项目。让我们看看它们的类型:

  • str 的类型为 char *(字面意思是“指向 char 的指针”)
  • NULL 是一个空指针常量(至少在我的系统上,它是 ((void*)0)
  • '\0' 是一个字符常量(它实际上是 < 类型) code>int,但不用担心;它通常用在需要 char 值的上下文中)

看到 char *void * 中的 * 了吗?这告诉编译器这些是指针类型(这是一种奇特的方式,表示这种类型的变量不保存值,它们只是指向它)。因此,当编译器看到 char *str 时,它知道您可能会要求执行诸如 *strstr[0] 之类的操作(两者都做同样的事情)。稍后我们会再讨论这个问题。

您会看到,当您在 C 程序中编写 str 时,编译器知道名为“str”的变量存储在内存位置中,例如 0x0001。它生成的代码转到 0x0001 并获取该值。这样,如果您执行类似的操作

str + 1

,则编译器将生成类似于以下内容的代码:

fetch the value from where str is stored (0x0001)
add 1 to that value

我确信您知道这一点。所以现在这行的意思应该很明显了:

str == NULL

由于 NULL 是一个空指针常量,所以该行测试 str 是否是一个空指针(即,一个不存在的指针) t 指向任何东西)。

因此,编译器通常会生成如下代码:

fetch the value from where str is stored
check if that value is 0

请记住,我们告诉编译器 str 实际上是一个指针类型。所以我们可以这样写:

*str

这使得编译器生成这样的:

fetch the value from where str is stored
now use that value as a memory address and fetch what is stored there

所以如果 str 持有 0x0200,那么我们将从内存地址 0x0200 获取值。请注意,编译器并不真正关心字符串是否真的存储在那里。

(我假设您知道 str[0]*str 相同。它可以更轻松地解释发生了什么。)

这个怎么样,然后?

*str == '\0'

因此,该行确实有效:

*str == (char) 0

这使得编译器生成以下内容:

fetch the value from where str is stored
now use that value like a memory address and fetch the char that is stored there
check if the value of that fetched char is 0

总结一下:

  • 编写 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 a char (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 type char * (literally, "pointer to a char")
  • NULL is a null pointer constant (at least on my system, it's ((void*)0))
  • '\0' is a character constant (it's actually of type int, but don't worry about that; it's generally used in a context that requires a char value)

See the * in char * and void *? 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 sees char *str, it knows that you might ask to do something like *str or str[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 like

str + 1

Then the compiler will generate code that looks something like:

fetch the value from where str is stored (0x0001)
add 1 to that value

Which is something I'm sure you know. So now it should be obvious what this line says:

str == NULL

Since NULL is a null pointer constant, that line tests whether str is a null pointer (i.e., a pointer that doesn't point to anything).

So the compiler typically generates code like this:

fetch the value from where str is stored
check if that value is 0

Remember now, if you please, that we told the compiler that str is really a pointer type. So we're allowed to write this:

*str

And that makes the compiler generate this:

fetch the value from where str is stored
now use that value as a memory address and fetch what is stored there

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?

*str == '\0'

So that line is really, in effect:

*str == (char) 0

Which makes the compiler generate this:

fetch the value from where str is stored
now use that value like a memory address and fetch the char that is stored there
check if the value of that fetched char is 0

To summarize:

  • Writing str == NULL tells you whether the pointer str is pointing at nothing.
  • Writing *str == '\0' tells you whether the pointer str 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.)

冷情妓 2024-12-25 11:51:33

本质上

  • str == NULL 确定 str 是否为 NULL 指针
  • str[0] == '\0'确定 str 是否为 0 长度的 c 样式字符串

当您组合它们时,您将检查它是否为 NULL 或空。这允许函数在方法开始时消除两种形式的空数据

Essentially

  • str == NULL determines if str is a NULL pointer
  • str[0] == '\0' determines if str is a 0 length c style string

When 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

奶茶白久 2024-12-25 11:51:33

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)

暮年慕年 2024-12-25 11:51:33

str==NULL 告诉您字符串是否为NULL

*str=='\0' 告诉您字符串长度是否为零。

注意:此答案是对 Mystical 的 15 秒答案的玩笑其中有 str=='\0'。当然,前 3 或 4 分钟内所做的更改不会显示出来,他已修复它ಠ_ಠ。

str==NULL tells you whether the string is NULL.

*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 ಠ_ಠ.

玩物 2024-12-25 11:51:33

str == NULL 表示“str 指向内存地址零”(或系统上任何为 NULL 的地址)。通常这意味着根本没有字符串。

str[0] == '\0' 表示“str 的第一个字符是字符零”(它标记字符串的结尾)。这意味着有一个字符串,但它是空的。想象一个空杯子与根本没有杯子的情况;同样的想法。

在其他语言中,您可能会编写 str == nullstr == ""。它们意味着两种不同的东西。了解 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 vs str == "". 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.

忆离笙 2024-12-25 11:51:33

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.

苯莒 2024-12-25 11:51:33
str == NULL 

意味着str没有指向任何地址=指针为空。

并且 str[0] == '\0' str 指向一个有效的地址,这一行检查第一个字符(即 str[0])是否为数字 0( ' 的 ascii 值) \0') 表示字符串结束。那么字符串是空的。 (str中没有字符:第一个是结束字符)

str == NULL 

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)

活雷疯 2024-12-25 11:51:33

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 pointer
2 -> str[0] == '\0' determines if str is a 0 length c style string

so 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..

空心↖ 2024-12-25 11:51:33

C# 中的等价物是:

if (string.IsNullOrEmpty(str))
{

}

简单的意思是字符串是否为 NULL 或者空字符串。

C# equivalent of it is:

if (string.IsNullOrEmpty(str))
{

}

Simple meaning is whether the string is NULL or an empty string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文