Difference between null and zero

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanaa
    New Member
    • Nov 2007
    • 44

    Difference between null and zero

    Hello.

    Can I have an elaboration on the difference between null and zero.

    For example, we say we set a byte to null (using bzero() to null), how different is it from setting the bytes to zero?

    Also, I'd love to be directed to some source which is explicit about the difference. I googled but, found nothing good enough.

    Thanks :)
    Hanaa
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The C Standard garantees that the numerical value 0 (zero) in a pointer context is converted to a NULL value. Bitwise speaking this NULL value can be interpreted as any numerical value but the C Standard garantees that the appropriate conversion takes place.

    This NULL pointer value always compares to a 'false' value in a boolean context (such as in an if-expression). You shouldn't care about the actual, internal, bit values of a NULL pointer, i.e. you can make one by using a numerical value 0 as in:

    Code:
    char* p= 0; // a NULL pointer;
    char* q= NULL; // also a NULL pointer.
    A NULL pointer is garanteed to point to nothing, i.e. no object. In 'normal' hardware a NULL pointer has all bits zero but it could have been 0xdeadbeef or any other value as well.

    kind regards,

    Jos

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      The term 'null' is overloaded in C. JosAH described what is meant by a 'null pointer'. Here are a few other usages of the word in C. For all I know, there could be additional usages in C++.

      1. The null character (used at the end of a null-terminated string).
      The null character is of type char and it must be encoded as the value 0. Notice that this must be true of whatever character encoding system (ASCII, EBCDIC, etc.) is used by any and all C compiler implementations . The proper way to indicate this value in your software is as character constant '\0'. The null character always compares to a 'false' value in a boolean context (such as in an if-expression). Notice that in the ASCII character encoding, the name of the null character is "NUL".

      2. The null wide character (used at the end of a wide string).
      The null wide character is of type wchar_t and it must be encoded as the value 0. The null wide character always compares to a 'false' value in a boolean context (such as in an if-expression).

      3. The null statement.
      The null statement consists simply of a semicolon. The null statement is used primarily in two situations. In the first case, a null body is used as the body of an iterative statement (while, do, or for). The second case is where a label is desired just before the right brace that terminates a compound statement. A label cannot simply precede the right brace, but must always be attached to a statement.

      Regarding null pointers, pedantically portable C code is careful to treat data pointers differently than function pointers. The C Standard permits an implementation to use different size pointers for each. The NULL macro has data pointer type void*. Therefore, you should not compare function pointer variables to NULL; instead compare against a literal "0". Unfortunately, Standard C doesn't provide a generic null function pointer macro. My personal practice is to provide macro FNULL (Function pointer NULL) with value "0".

      Comment

      • hanaa
        New Member
        • Nov 2007
        • 44

        #4
        Originally posted by donbock
        1. The null character (used at the end of a null-terminated string).
        The null character is of type char and it must be encoded as the value 0.
        What does "encoding as 0" mean? Does it mean that the 0 is stored in the memory, in the place of the character that we are setting to null?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by hanaa
          What does "encoding as 0" mean? Does it mean that the 0 is stored in the memory, in the place of the character that we are setting to null?
          For instance, if your compiler implementation uses the ASCII encoding standard for characters, then character constant 'A' is encoded [stored] as 0x41. On the other hand, if your compiler implementation uses the EBCDIC encoding standard for characters, then character constant 'A' is encoded [stored] as 0xC1.

          My point was that the C Standard requires that encoded value 0 be reserved for the null character in all encoding standards. An encoding standard that requires 0 to mean something else would be unsuitable for use by any C compiler implementation.

          Comment

          • hanaa
            New Member
            • Nov 2007
            • 44

            #6
            Got it. Thanks. :-)

            Comment

            Working...