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

前端 未结 11 1445
無奈伤痛
無奈伤痛 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 16:59

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

    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.

提交回复
热议问题