Also, C strings are NUL-terminated, not NULL-terminated. (char)(0) is the NUL character; (void * )(0) is the NULL pointer (usually).
If (NULL == 0) isn't true you're not using C.
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.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.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).
I honestly don't know whether POSIX/SUS mandates ASCII.
char c = 65
is equivalent to and just as legal as char c = 'A'
So yes, C strings ARE zero-terminated. In fact, it is a bit weird to say they are null-terminated. It is technically true, given that NULL often translates to 0, but NULL is a pointer value in the same way that zero is a number.For some reason, I vaguely remember some security hack in one OS where NULL went to a special place in memory that was marked as invalid (1, 63, or something), so in that case, NULL was not defined as 0. Since the memory is vague, though, don't quote me on it.
Yeah, it would be. They're NUL-terminated.
http://googlefight.com/index.php?lang=en_GB&word1=%22nul...
I hate all the variations of nothing. undef, null, NULL, NIL, NUL...
The character shorthand is "NUL", but the full name is "null". I still stand by my (silly semantic) point that the use of NULL is overloaded here. NULL is a pointer value, not a character value. The string does not terminate at a pointer; it terminates with a magic character. This has, in fact, bitten me in the past where '\0' was a legitimate member of my string, and I have to write my own accessors.
As for the NULL, it needn't be a pointer value, the standard permits it being "an integral constant expression with the value 0", so in certain implementations it can be the same as '\0'.