What is the difference between NULL, '\0' and 0?

前端 未结 11 1250
無奈伤痛
無奈伤痛 2020-11-21 16:32

In C, there appear to be differences between various values of zero -- NULL, NUL and 0.

I know that the ASCII character

相关标签:
11条回答
  • 2020-11-21 17:00

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

    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.

    0 讨论(0)
  • 2020-11-21 17:02

    Note: This answer applies to the C language, not 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.

    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.

    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.

    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.

    if (pointer == 0)
    

    0 is another representation of the null pointer constant.

    if (!pointer)
    

    This if statement implicitly checks "is not 0", so we reverse that to mean "is 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.

    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.

    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:

    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.").

    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.

    0 讨论(0)
  • 2020-11-21 17:02

    If NULL and 0 are equivalent as null pointer constants, which should I use? in the C FAQ list addresses this issue as well:

    C programmers must understand that NULL and 0 are interchangeable in pointer contexts, and that an uncast 0 is perfectly acceptable. Any usage of NULL (as opposed to 0) 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 pointer 0's from integer 0's.

    It is only in pointer contexts that NULL and 0 are equivalent. NULL should not be used when another kind of 0 is required, even though it might work, because doing so sends the wrong stylistic message. (Furthermore, ANSI allows the definition of NULL to be ((void *)0), which will not work at all in non-pointer contexts.) In particular, do not use NULL when the ASCII null character (NUL) is desired. Provide your own definition

    #define NUL '\0'
    

    if you must.

    0 讨论(0)
  • 2020-11-21 17:09

    A one-L NUL, it ends a string.

    A two-L NULL points to no thing.

    And I will bet a golden bull

    That there is no three-L NULLL.

    How do you deal with NUL?

    0 讨论(0)
  • 2020-11-21 17:13

    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.

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

    The actual value a given system or database file format uses to represent a NULL isn't necessarily 0x00.

    0 讨论(0)
提交回复
热议问题