4 ms·
'\0' is a character, not a string. The string is "\0". Also, C strings are NUL-terminated, not NULL-terminated. (char)(0) is the NUL character; (void * )(0)
by cperciva 16y ago
'\0' is a character, not a string. The string is "\0".
Also, C strings are NUL-terminated, not NULL-terminated. (char)(0) is the NUL character; (void * )(0) is the NULL pointer (usually).
- avar 16y agoActually it's the NULL pointer by definition. 0 is always equivalent to NULL in pointer context, even though you're working with some hypothetical crazy system where NULL is actually #define NULL 1337. It's mandated by the standard. If (NULL == 0) isn't true you're not using C.
- cperciva 16y agoWhoops, I forgot the point I was trying to make. NULL is (void * )(0), but the code void * p; memset(&p, 0, sizeof(p)); assert(p == NULL); isn't necessarily valid, since the binary representation of NULL isn't necessarily as a sequence of zeroes -- even though everybody writes code which assumes that it is.
- avar 16y agoYeah, that's unportable. Although I'm not aware of any system where it doesn't work. For the record, regarding 0 in pointer context the C standard has this to say: An integral constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant. If a null pointer constant is assigned to or compared for equality to a pointer, the constant is converted to a pointer of that type. Such a pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function. -- C89/C90 section 3.2.2.3 Pointers The standard then goes on to say that NULL is a macro that's guaranteed to expand to a null pointer constant.
- cperciva 16y agoYeah, that's unportable. Although I'm not aware of any system where it doesn't work. Right. I was considering changing this in FreeBSD last year in order to prevent map-at-NULL kernel exploits, but cooler heads prevailed (and pointed out to me how much code such a change would break).
- nuxi 16y agoHere's the historical list of machines using nonzero null pointers, from comp.lang.c FAQ: http://linuxdude.com/Steve_Sumit/C-faq/q5.17.html http://linuxdude.com/Steve_Sumit/C-faq/q5.17.html
- tedunangst 16y agoThe C standard exclusively uses the phrase "null-terminated" to refer to strings.
- cperciva 16y agoHuh, you're right. I wonder when they changed that.
- jonsen 16y agoNUL is in ASCII an abbreviation for null.
- nuxi 16y agoThey didn't, it's there from at least ANSI C89 (ISO C90): "A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string literal."
- JoachimSchipper 16y agoI think the rationale is in EBCDIC support; the C standard didn't want to/couldn't mandate ASCII, and therefore could not use the ASCII NUL character. I honestly don't know whether POSIX/SUS mandates ASCII.
- nuxi 16y agoAs far as I understand, no: http://www.opengroup.org/onlinepubs/000095399/xrat/xbd_chap06.html http://www.opengroup.org/onlinepubs/000095399/xrat/xbd_chap0...
- JoachimSchipper 16y agoI read that as "ASCII must be supported, do whatever else you want". But I don't want to spend too much time on this...