In C, there appear to be differences between various values of zero -- NULL
, NUL
and 0
.
在C中,不同值之间似乎存在差异——NULL, NUL和0。
I know that the ASCII character '0'
evaluates to 48
or 0x30
.
我知道ASCII字符'0'的值是48或0x30。
The NULL
pointer is usually defined as:
空指针通常定义为:
#define NULL 0
Or
或
#define NULL (void *)0
In addition, there is the NUL
character '\0'
which seems to evaluate to 0
as well.
此外,还有NUL字符'\0',它似乎也值为0。
Are there times when these three values can not be equal?
是否存在这三个值不相等的时刻?
Is this also true on 64 bit systems?
在64位系统上也是这样吗?
11 个解决方案
#1
280
Note: This answer applies to the C language, not C++.
注意:这个答案适用于C语言,而不是c++。
Null Pointers
The integer constant literal 0
has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0
, it is just described in different ways.
integer常量literal 0根据使用的上下文有不同的含义。在所有情况下,它仍然是一个值为0的整数常量,它只是用不同的方式描述。
If a pointer is being compared to the constant literal 0
, then this is a check to see if the pointer is a null pointer. This 0
is then referred to as a null pointer constant. The C standard defines that 0
cast to the type void *
is both a null pointer and a null pointer constant.
如果将指针与常量文字0进行比较,那么这是一个检查指针是否为空指针的检查。这个0被称为空指针常量。C标准定义了0转换到类型void *既是空指针,也是空指针常量。
Additionally, to help readability, the macro NULL
is provided in the header file stddef.h
. Depending upon your compiler it might be possible to #undef NULL
and redefine it to something wacky.
此外,为了帮助可读性,在头文件stddef.h中提供了宏NULL。根据您的编译器,可以将undef NULL重新定义为古怪的东西。
Therefore, here are some valid ways to check for a null pointer:
因此,这里有一些有效的方法来检查空指针:
if (pointer == NULL)
NULL
is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL
is, as long as it is a valid null pointer constant.
NULL被定义为与空指针比较。它是实现定义了NULL的实际定义,只要它是有效的空指针常量。
if (pointer == 0)
0
is another representation of the null pointer constant.
0是空指针常量的另一个表示。
if (!pointer)
This if
statement implicitly checks "is not 0", so we reverse that to mean "is 0".
如果语句隐式检查“不是0”,那么我们将其反向为“为0”。
The following are INVALID ways to check for a null pointer:
以下是检查空指针的无效方法:
int mynull = 0;
<some code>
if (pointer == mynull)
To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.
对于编译器,这不是一个空指针的检查,而是两个变量的等式检查。如果mynull在代码中没有变化,编译器优化常数将0在if语句中加入0,这可能会起作用,但这并没有保证,而且编译器必须根据C标准生成至少一个诊断消息(警告或错误)。
Note that what is a null pointer in the C language. It does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.
注意,在C语言中什么是空指针。它对底层架构没有影响。如果底层架构的空指针值定义为地址0xDEADBEEF,那么就由编译器来处理这个问题。
As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:
因此,即使在这个有趣的架构中,以下方法仍然是检查空指针的有效方法:
if (!pointer)
if (pointer == NULL)
if (pointer == 0)
The following are INVALID ways to check for a null pointer:
以下是检查空指针的无效方法:
#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)
as these are seen by a compiler as normal comparisons.
因为这些被编译器看作是正常的比较。
Null Characters
'\0'
is defined to be a null character - that is a character with all bits set to zero. This has nothing to do with pointers. However you may see something similar to this code:
“\0”被定义为一个空字符——这是一个所有位都设置为零的字符。这与指针无关。不过,您可能会看到类似的代码:
if (!*string_pointer)
checks if the string pointer is pointing at a null character
检查字符串指针是否指向一个空字符。
if (*string_pointer)
checks if the string pointer is pointing at a non-null character
检查字符串指针是否指向非空字符。
Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.
不要把这些与空指针混淆。仅仅因为位表示是相同的,这允许一些方便的交叉,它们实际上不是一回事。
Additionally, '\0'
is (like all character literals) an integer constant, in this case with the value zero. So '\0'
is completely equivalent to an unadorned 0
integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").
另外,'\0'是(像所有字符文字一样)一个整数常量,在这个例子中值为零。因此,“\0”完全等价于一个不加修饰的0整数常量——惟一的区别在于它向一个人读者传达的意图(“我用这个作为空字符”)。
References
See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.
参见问题5.3。c FAQ。请参阅此pdf的C标准。请参阅第6.3.2.3节指示,第3段。
#2
27
It appears that a number of people misunderstand what the differences between NULL, '\0' and 0 are. So, to explain, and in attempt to avoid repeating things said earlier:
似乎有许多人误解了NULL、“\0”和“0”之间的区别。所以,为了解释,为了避免重复之前说过的话:
A constant expression of type int with the value 0, or an expression of this type, cast to type void * is a null pointer constant, which if converted to a pointer becomes a null pointer. It is guaranteed by the standard to compare unequal to any pointer to any object or function.
类型int类型的常量表达式为0,或该类型的表达式,cast to类型void *是一个空指针常量,如果转换为一个指针,它将变成一个空指针。它由标准来保证,比较不等于任何对象或函数的指针。
NULL is a macro, defined in as a null pointer constant.
NULL是一个宏,定义为空指针常量。
'\0' is a construction used to represent the null character, used to terminate a string.
“\0”是用来表示空字符的结构,用于终止字符串。
A null character is a byte which has all its bits set to 0.
null字符是一个字节,它的所有位都设置为0。
#3
15
All three define the meaning of zero in different context.
这三个定义在不同的背景下定义了零的意义。
- pointer context - NULL is used and means the value of the pointer is 0, independent of whether it is 32bit or 64bit (one case 4 bytes the other 8 bytes of zeroes).
- 指针上下文- NULL被使用,意味着指针的值是0,独立于它是32位还是64位(1个case 4字节,其他8字节的0)。
- string context - the character representing the digit zero has a hex value of 0x30, whereas the NUL character has hex value of 0x00 (used for terminating strings).
- 字符串上下文——表示数字零的字符具有0x30的十六进制值,而NUL字符的十六进制值为0x00(用于终止字符串)。
These three are always different when you look at the memory:
当你看记忆的时候,这三个总是不同的:
NULL - 0x00000000 or 0x00000000'00000000 (32 vs 64 bit)
NUL - 0x00 or 0x0000 (ascii vs 2byte unicode)
'0' - 0x20
I hope this clarifies it.
我希望这能澄清事实。
#4
5
If NULL and 0 are equivalent as null pointer constants, which should I use? in the C FAQ list addresses this issue as well:
如果NULL和0等价为空指针常量,我应该使用哪个?在cfaq列表中也解决了这个问题:
C programmers must understand that
NULL
and0
are interchangeable in pointer contexts, and that an uncast0
is perfectly acceptable. Any usage of NULL (as opposed to0
) should be considered a gentle reminder that a pointer is involved; programmers should not depend on it (either for their own understanding or the compiler's) for distinguishing pointer0
's from integer0
's.C程序员必须明白,在指针的上下文中,NULL和0是可以互换的,而uncast 0是完全可以接受的。任何使用NULL(相对于0)应该被看作是一个温和的提示,提示有一个指针;程序员不应该依赖它(不是为了自己的理解,也不是为了编译器)来区分指针0和整数0。
It is only in pointer contexts that
NULL
and0
are equivalent.NULL
should not be used when another kind of0
is required, even though it might work, because doing so sends the wrong stylistic message. (Furthermore, ANSI allows the definition ofNULL
to be((void *)0)
, which will not work at all in non-pointer contexts.) In particular, do not useNULL
when the ASCII null character (NUL
) is desired. Provide your own definition只有在指针上下文中,NULL和0是等价的。当需要其他类型的0时,不应该使用NULL,即使它可能起作用,因为这样做会发送错误的样式信息。(此外,ANSI允许将NULL定义为((void *)0),这在非指针上下文中完全不起作用。)特别是,当需要ASCII NULL字符时,不要使用NULL。提供自己的定义
#define NUL '\0'
if you must.
如果你必须。
#5
5
What is the difference between NULL, ‘\0’ and 0
0和0之间的区别是什么?
"null character (NUL)" is easiest to rule out. '\0'
is a character literal. In C, it is implemented as int
, so, it's the same as 0, which is of INT_TYPE_SIZE
. In C++, character literal is implemented as char
, which is 1 byte. This is normally different from NULL
or 0
.
“空字符(NUL)”是最容易排除的。“\0”是一个字符文字。在C中,它被实现为int,因此,它与INT_TYPE_SIZE的0相同。在c++中,字符文字被实现为char,即1个字节。这通常不同于NULL或0。
Next, NULL
is a pointer value that specifies that a variable does not point to any address space. Set aside the fact that it is usually implemented as zeros, it must be able to express the full address space of the architecture. Thus, on a 32-bit architecture NULL (likely) is 4-byte and on 64-bit architecture 8-byte. This is up to the implementation of C.
接下来,NULL是一个指针值,它指定变量不指向任何地址空间。抛开它通常被实现为零的事实,它必须能够表达体系结构的完整地址空间。因此,在32位体系结构上,NULL(可能)是4字节和64位架构8字节。这取决于C的实现。
Finally, the literal 0
is of type int
, which is of size INT_TYPE_SIZE
. The default value of INT_TYPE_SIZE
could be different depending on architecture.
最后,文字0是int类型,大小为INT_TYPE_SIZE。INT_TYPE_SIZE的默认值可能因架构不同而不同。
Apple wrote:
苹果写道:
The 64-bit data model used by Mac OS X is known as "LP64". This is the common data model used by other 64-bit UNIX systems from Sun and SGI as well as 64-bit Linux. The LP64 data model defines the primitive types as follows:
Mac OS X使用的64位数据模型被称为“LP64”。这是Sun、SGI以及64位Linux的其他64位UNIX系统使用的通用数据模型。LP64数据模型定义了以下基本类型:
- ints are 32-bit
- 整数是32位
- longs are 64-bit
- 多头是64位
- long-longs are also 64-bit
- long-longs也是64位
- pointers are 64-bit
- 指针是64位
Wikipedia 64-bit:
*64位:
Microsoft's VC++ compiler uses the LLP64 model.
微软的vc++编译器使用LLP64模型。
64-bit data models
Data model short int long long long pointers Sample operating systems
LLP64 16 32 32 64 64 Microsoft Win64 (X64/IA64)
LP64 16 32 64 64 64 Most Unix and Unix-like systems (Solaris, Linux, etc.)
ILP64 16 64 64 64 64 HAL
SILP64 64 64 64 64 64 ?
Edit: Added more on the character literal.
编辑:添加更多的字符文字。
#include <stdio.h>
int main(void) {
printf("%d", sizeof('\0'));
return 0;
}
The above code returns 4 on gcc and 1 on g++.
上面的代码在gcc中返回4,在g++上返回1。
#6
2
A one-L NUL, it ends a string.
一个l NUL,它结束一个字符串。
A two-L NULL points to no thing.
一个2 - l的空值。
And I will bet a golden bull
我敢打赌一只金牛。
That there is no three-L NULLL.
没有3 - l零。
你如何处理NUL?
#7
2
One good piece which helps me when starting with C(Taken from the Expert C Programming by Linden)
从C开始(从Linden的专家C编程开始)
The One 'l' nul and the Two 'l' null
一个'l' nul和两个'l' null。
Memorize this little rhyme to recall the correct terminology for pointers and ASCII zero:
记住这个小韵文,回忆一下指针和ASCII 0的正确术语:
The one "l" NUL ends an ASCII string,
The two "l" NULL points to no thing.
Apologies to Ogden Nash, but the three "l" nulll means check your spelling.
The ASCII character with the bit pattern of zero is termed a "NUL". The special pointer value that means the pointer points nowhere is "NULL". The two terms are not interchangeable in meaning.
带有位模式为0的ASCII字符被称为“NUL”。特殊的指针值,指的是指针指向的任何地方都是“NULL”。这两项在意义上不能互换。
#8
1
"NUL" is not 0, but refers to the ASCII NUL character. At least, that's how I've seen it used. The null pointer is often defined as 0, but this depends on the environment you are running in, and the specification of whatever operating system or language you are using.
“NUL”不是0,而是指ASCII字符。至少我是这么看的。空指针通常被定义为0,但这取决于您正在运行的环境,以及您正在使用的任何操作系统或语言的规范。
In ANSI C, the null pointer is specified as the integer value 0. So any world where that's not true is not ANSI C compliant.
在ANSI C中,空指针被指定为整数值0。所以任何不真实的世界都不是ANSI C兼容的。
#9
0
A byte with a value of 0x00 is, on the ASCII table, the special character called "NUL" or "NULL". In C, since you shouldn't embed control characters in your source code, this is represented in C strings with an escaped 0, i.e., "\0".
一个值为0x00的字节是在ASCII表上,称为“NUL”或“NULL”的特殊字符。在C语言中,由于您不应该在源代码中嵌入控制字符,所以在C字符串中表示的是一个转义为0的字符串,即。,“\ 0”。
But a true NULL is not a value. It is the absence of a value. For a pointer, it means the pointer has nothing to point to. In a database, it means there is no value in a field (which is not the same thing as saying the field is blank, 0, or filled with spaces).
但是一个真正的空值不是一个值。它是没有价值的。对于一个指针,它意味着指针没有指向。在数据库中,它意味着字段中没有值(这与说字段为空、0或填充空格是不一样的)。
The actual value a given system or database file format uses to represent a NULL isn't necessarily 0x00.
给定系统或数据库文件格式用来表示NULL的实际值不一定是0x00。
#10
-1
NULL
is not guaranteed to be 0 -- its exact value is architecture-dependent. Most major architectures define it to (void*)0
.
NULL不能保证为0——它的确切值是与体系结构相关的。大多数主要的体系结构都将其定义为(void*)0。
'\0'
will always equal 0, because that is how byte 0 is encoded in a character literal.
'\0'将始终等于0,因为这就是字节0编码在字符文字中的方式。
I don't remember whether C compilers are required to use ASCII -- if not, '0'
might not always equal 48. Regardless, it's unlikely you'll ever encounter a system which uses an alternative character set like EBCDIC unless you're working on very obscure systems.
我不记得是否需要C编译器来使用ASCII——如果不是的话,“0”可能不总是等于48。无论如何,你不太可能遇到一个系统,它使用一个像EBCDIC这样的替代字符集,除非你在一个非常模糊的系统上工作。
The sizes of the various types will differ on 64-bit systems, but the integer values will be the same.
不同类型的大小在64位系统上是不同的,但是整型值是相同的。
Some commenters have expressed doubt that NULL be equal to 0, but not be zero. Here is an example program, along with expected output on such a system:
一些评论者表示怀疑零等于0,但不等于零。下面是一个示例程序,以及这样一个系统的预期输出:
#include <stdio.h>
int main () {
size_t ii;
int *ptr = NULL;
unsigned long *null_value = (unsigned long *)&ptr;
if (NULL == 0) {
printf ("NULL == 0\n"); }
printf ("NULL = 0x");
for (ii = 0; ii < sizeof (ptr); ii++) {
printf ("%02X", null_value[ii]); }
printf ("\n");
return 0;
}
That program could print:
这个项目可以打印:
NULL == 0
NULL = 0x00000001
#11
-2
(void*) 0 is NULL, and '\0' represents the end of a string.
(void*) 0为空,“\0”表示字符串的结束。
#1
280
Note: This answer applies to the C language, not C++.
注意:这个答案适用于C语言,而不是c++。
Null Pointers
The integer constant literal 0
has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0
, it is just described in different ways.
integer常量literal 0根据使用的上下文有不同的含义。在所有情况下,它仍然是一个值为0的整数常量,它只是用不同的方式描述。
If a pointer is being compared to the constant literal 0
, then this is a check to see if the pointer is a null pointer. This 0
is then referred to as a null pointer constant. The C standard defines that 0
cast to the type void *
is both a null pointer and a null pointer constant.
如果将指针与常量文字0进行比较,那么这是一个检查指针是否为空指针的检查。这个0被称为空指针常量。C标准定义了0转换到类型void *既是空指针,也是空指针常量。
Additionally, to help readability, the macro NULL
is provided in the header file stddef.h
. Depending upon your compiler it might be possible to #undef NULL
and redefine it to something wacky.
此外,为了帮助可读性,在头文件stddef.h中提供了宏NULL。根据您的编译器,可以将undef NULL重新定义为古怪的东西。
Therefore, here are some valid ways to check for a null pointer:
因此,这里有一些有效的方法来检查空指针:
if (pointer == NULL)
NULL
is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL
is, as long as it is a valid null pointer constant.
NULL被定义为与空指针比较。它是实现定义了NULL的实际定义,只要它是有效的空指针常量。
if (pointer == 0)
0
is another representation of the null pointer constant.
0是空指针常量的另一个表示。
if (!pointer)
This if
statement implicitly checks "is not 0", so we reverse that to mean "is 0".
如果语句隐式检查“不是0”,那么我们将其反向为“为0”。
The following are INVALID ways to check for a null pointer:
以下是检查空指针的无效方法:
int mynull = 0;
<some code>
if (pointer == mynull)
To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.
对于编译器,这不是一个空指针的检查,而是两个变量的等式检查。如果mynull在代码中没有变化,编译器优化常数将0在if语句中加入0,这可能会起作用,但这并没有保证,而且编译器必须根据C标准生成至少一个诊断消息(警告或错误)。
Note that what is a null pointer in the C language. It does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.
注意,在C语言中什么是空指针。它对底层架构没有影响。如果底层架构的空指针值定义为地址0xDEADBEEF,那么就由编译器来处理这个问题。
As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:
因此,即使在这个有趣的架构中,以下方法仍然是检查空指针的有效方法:
if (!pointer)
if (pointer == NULL)
if (pointer == 0)
The following are INVALID ways to check for a null pointer:
以下是检查空指针的无效方法:
#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)
as these are seen by a compiler as normal comparisons.
因为这些被编译器看作是正常的比较。
Null Characters
'\0'
is defined to be a null character - that is a character with all bits set to zero. This has nothing to do with pointers. However you may see something similar to this code:
“\0”被定义为一个空字符——这是一个所有位都设置为零的字符。这与指针无关。不过,您可能会看到类似的代码:
if (!*string_pointer)
checks if the string pointer is pointing at a null character
检查字符串指针是否指向一个空字符。
if (*string_pointer)
checks if the string pointer is pointing at a non-null character
检查字符串指针是否指向非空字符。
Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.
不要把这些与空指针混淆。仅仅因为位表示是相同的,这允许一些方便的交叉,它们实际上不是一回事。
Additionally, '\0'
is (like all character literals) an integer constant, in this case with the value zero. So '\0'
is completely equivalent to an unadorned 0
integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").
另外,'\0'是(像所有字符文字一样)一个整数常量,在这个例子中值为零。因此,“\0”完全等价于一个不加修饰的0整数常量——惟一的区别在于它向一个人读者传达的意图(“我用这个作为空字符”)。
References
See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.
参见问题5.3。c FAQ。请参阅此pdf的C标准。请参阅第6.3.2.3节指示,第3段。
#2
27
It appears that a number of people misunderstand what the differences between NULL, '\0' and 0 are. So, to explain, and in attempt to avoid repeating things said earlier:
似乎有许多人误解了NULL、“\0”和“0”之间的区别。所以,为了解释,为了避免重复之前说过的话:
A constant expression of type int with the value 0, or an expression of this type, cast to type void * is a null pointer constant, which if converted to a pointer becomes a null pointer. It is guaranteed by the standard to compare unequal to any pointer to any object or function.
类型int类型的常量表达式为0,或该类型的表达式,cast to类型void *是一个空指针常量,如果转换为一个指针,它将变成一个空指针。它由标准来保证,比较不等于任何对象或函数的指针。
NULL is a macro, defined in as a null pointer constant.
NULL是一个宏,定义为空指针常量。
'\0' is a construction used to represent the null character, used to terminate a string.
“\0”是用来表示空字符的结构,用于终止字符串。
A null character is a byte which has all its bits set to 0.
null字符是一个字节,它的所有位都设置为0。
#3
15
All three define the meaning of zero in different context.
这三个定义在不同的背景下定义了零的意义。
- pointer context - NULL is used and means the value of the pointer is 0, independent of whether it is 32bit or 64bit (one case 4 bytes the other 8 bytes of zeroes).
- 指针上下文- NULL被使用,意味着指针的值是0,独立于它是32位还是64位(1个case 4字节,其他8字节的0)。
- string context - the character representing the digit zero has a hex value of 0x30, whereas the NUL character has hex value of 0x00 (used for terminating strings).
- 字符串上下文——表示数字零的字符具有0x30的十六进制值,而NUL字符的十六进制值为0x00(用于终止字符串)。
These three are always different when you look at the memory:
当你看记忆的时候,这三个总是不同的:
NULL - 0x00000000 or 0x00000000'00000000 (32 vs 64 bit)
NUL - 0x00 or 0x0000 (ascii vs 2byte unicode)
'0' - 0x20
I hope this clarifies it.
我希望这能澄清事实。
#4
5
If NULL and 0 are equivalent as null pointer constants, which should I use? in the C FAQ list addresses this issue as well:
如果NULL和0等价为空指针常量,我应该使用哪个?在cfaq列表中也解决了这个问题:
C programmers must understand that
NULL
and0
are interchangeable in pointer contexts, and that an uncast0
is perfectly acceptable. Any usage of NULL (as opposed to0
) should be considered a gentle reminder that a pointer is involved; programmers should not depend on it (either for their own understanding or the compiler's) for distinguishing pointer0
's from integer0
's.C程序员必须明白,在指针的上下文中,NULL和0是可以互换的,而uncast 0是完全可以接受的。任何使用NULL(相对于0)应该被看作是一个温和的提示,提示有一个指针;程序员不应该依赖它(不是为了自己的理解,也不是为了编译器)来区分指针0和整数0。
It is only in pointer contexts that
NULL
and0
are equivalent.NULL
should not be used when another kind of0
is required, even though it might work, because doing so sends the wrong stylistic message. (Furthermore, ANSI allows the definition ofNULL
to be((void *)0)
, which will not work at all in non-pointer contexts.) In particular, do not useNULL
when the ASCII null character (NUL
) is desired. Provide your own definition只有在指针上下文中,NULL和0是等价的。当需要其他类型的0时,不应该使用NULL,即使它可能起作用,因为这样做会发送错误的样式信息。(此外,ANSI允许将NULL定义为((void *)0),这在非指针上下文中完全不起作用。)特别是,当需要ASCII NULL字符时,不要使用NULL。提供自己的定义
#define NUL '\0'
if you must.
如果你必须。
#5
5
What is the difference between NULL, ‘\0’ and 0
0和0之间的区别是什么?
"null character (NUL)" is easiest to rule out. '\0'
is a character literal. In C, it is implemented as int
, so, it's the same as 0, which is of INT_TYPE_SIZE
. In C++, character literal is implemented as char
, which is 1 byte. This is normally different from NULL
or 0
.
“空字符(NUL)”是最容易排除的。“\0”是一个字符文字。在C中,它被实现为int,因此,它与INT_TYPE_SIZE的0相同。在c++中,字符文字被实现为char,即1个字节。这通常不同于NULL或0。
Next, NULL
is a pointer value that specifies that a variable does not point to any address space. Set aside the fact that it is usually implemented as zeros, it must be able to express the full address space of the architecture. Thus, on a 32-bit architecture NULL (likely) is 4-byte and on 64-bit architecture 8-byte. This is up to the implementation of C.
接下来,NULL是一个指针值,它指定变量不指向任何地址空间。抛开它通常被实现为零的事实,它必须能够表达体系结构的完整地址空间。因此,在32位体系结构上,NULL(可能)是4字节和64位架构8字节。这取决于C的实现。
Finally, the literal 0
is of type int
, which is of size INT_TYPE_SIZE
. The default value of INT_TYPE_SIZE
could be different depending on architecture.
最后,文字0是int类型,大小为INT_TYPE_SIZE。INT_TYPE_SIZE的默认值可能因架构不同而不同。
Apple wrote:
苹果写道:
The 64-bit data model used by Mac OS X is known as "LP64". This is the common data model used by other 64-bit UNIX systems from Sun and SGI as well as 64-bit Linux. The LP64 data model defines the primitive types as follows:
Mac OS X使用的64位数据模型被称为“LP64”。这是Sun、SGI以及64位Linux的其他64位UNIX系统使用的通用数据模型。LP64数据模型定义了以下基本类型:
- ints are 32-bit
- 整数是32位
- longs are 64-bit
- 多头是64位
- long-longs are also 64-bit
- long-longs也是64位
- pointers are 64-bit
- 指针是64位
Wikipedia 64-bit:
*64位:
Microsoft's VC++ compiler uses the LLP64 model.
微软的vc++编译器使用LLP64模型。
64-bit data models
Data model short int long long long pointers Sample operating systems
LLP64 16 32 32 64 64 Microsoft Win64 (X64/IA64)
LP64 16 32 64 64 64 Most Unix and Unix-like systems (Solaris, Linux, etc.)
ILP64 16 64 64 64 64 HAL
SILP64 64 64 64 64 64 ?
Edit: Added more on the character literal.
编辑:添加更多的字符文字。
#include <stdio.h>
int main(void) {
printf("%d", sizeof('\0'));
return 0;
}
The above code returns 4 on gcc and 1 on g++.
上面的代码在gcc中返回4,在g++上返回1。
#6
2
A one-L NUL, it ends a string.
一个l NUL,它结束一个字符串。
A two-L NULL points to no thing.
一个2 - l的空值。
And I will bet a golden bull
我敢打赌一只金牛。
That there is no three-L NULLL.
没有3 - l零。
你如何处理NUL?
#7
2
One good piece which helps me when starting with C(Taken from the Expert C Programming by Linden)
从C开始(从Linden的专家C编程开始)
The One 'l' nul and the Two 'l' null
一个'l' nul和两个'l' null。
Memorize this little rhyme to recall the correct terminology for pointers and ASCII zero:
记住这个小韵文,回忆一下指针和ASCII 0的正确术语:
The one "l" NUL ends an ASCII string,
The two "l" NULL points to no thing.
Apologies to Ogden Nash, but the three "l" nulll means check your spelling.
The ASCII character with the bit pattern of zero is termed a "NUL". The special pointer value that means the pointer points nowhere is "NULL". The two terms are not interchangeable in meaning.
带有位模式为0的ASCII字符被称为“NUL”。特殊的指针值,指的是指针指向的任何地方都是“NULL”。这两项在意义上不能互换。
#8
1
"NUL" is not 0, but refers to the ASCII NUL character. At least, that's how I've seen it used. The null pointer is often defined as 0, but this depends on the environment you are running in, and the specification of whatever operating system or language you are using.
“NUL”不是0,而是指ASCII字符。至少我是这么看的。空指针通常被定义为0,但这取决于您正在运行的环境,以及您正在使用的任何操作系统或语言的规范。
In ANSI C, the null pointer is specified as the integer value 0. So any world where that's not true is not ANSI C compliant.
在ANSI C中,空指针被指定为整数值0。所以任何不真实的世界都不是ANSI C兼容的。
#9
0
A byte with a value of 0x00 is, on the ASCII table, the special character called "NUL" or "NULL". In C, since you shouldn't embed control characters in your source code, this is represented in C strings with an escaped 0, i.e., "\0".
一个值为0x00的字节是在ASCII表上,称为“NUL”或“NULL”的特殊字符。在C语言中,由于您不应该在源代码中嵌入控制字符,所以在C字符串中表示的是一个转义为0的字符串,即。,“\ 0”。
But a true NULL is not a value. It is the absence of a value. For a pointer, it means the pointer has nothing to point to. In a database, it means there is no value in a field (which is not the same thing as saying the field is blank, 0, or filled with spaces).
但是一个真正的空值不是一个值。它是没有价值的。对于一个指针,它意味着指针没有指向。在数据库中,它意味着字段中没有值(这与说字段为空、0或填充空格是不一样的)。
The actual value a given system or database file format uses to represent a NULL isn't necessarily 0x00.
给定系统或数据库文件格式用来表示NULL的实际值不一定是0x00。
#10
-1
NULL
is not guaranteed to be 0 -- its exact value is architecture-dependent. Most major architectures define it to (void*)0
.
NULL不能保证为0——它的确切值是与体系结构相关的。大多数主要的体系结构都将其定义为(void*)0。
'\0'
will always equal 0, because that is how byte 0 is encoded in a character literal.
'\0'将始终等于0,因为这就是字节0编码在字符文字中的方式。
I don't remember whether C compilers are required to use ASCII -- if not, '0'
might not always equal 48. Regardless, it's unlikely you'll ever encounter a system which uses an alternative character set like EBCDIC unless you're working on very obscure systems.
我不记得是否需要C编译器来使用ASCII——如果不是的话,“0”可能不总是等于48。无论如何,你不太可能遇到一个系统,它使用一个像EBCDIC这样的替代字符集,除非你在一个非常模糊的系统上工作。
The sizes of the various types will differ on 64-bit systems, but the integer values will be the same.
不同类型的大小在64位系统上是不同的,但是整型值是相同的。
Some commenters have expressed doubt that NULL be equal to 0, but not be zero. Here is an example program, along with expected output on such a system:
一些评论者表示怀疑零等于0,但不等于零。下面是一个示例程序,以及这样一个系统的预期输出:
#include <stdio.h>
int main () {
size_t ii;
int *ptr = NULL;
unsigned long *null_value = (unsigned long *)&ptr;
if (NULL == 0) {
printf ("NULL == 0\n"); }
printf ("NULL = 0x");
for (ii = 0; ii < sizeof (ptr); ii++) {
printf ("%02X", null_value[ii]); }
printf ("\n");
return 0;
}
That program could print:
这个项目可以打印:
NULL == 0
NULL = 0x00000001
#11
-2
(void*) 0 is NULL, and '\0' represents the end of a string.
(void*) 0为空,“\0”表示字符串的结束。