News for hackers. Stuff that matters.
Re:Interviewing programmers: coding test example explained by pvdm on Thursday June 03, 2010 @10:08AM (#1401327)
'\0' is null-terminated NOT zero-terminated, no ? By definition, a string in C is always null-terminated.
Re:Interviewing programmers: coding test example explained by cperciva on Thursday June 03, 2010 @10:15AM (#1401353)
'\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).

Re:Interviewing programmers: coding test example explained by avar on Thursday June 03, 2010 @11:24AM (#1401553)
Actually 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.

Re:Interviewing programmers: coding test example explained by cperciva on Thursday June 03, 2010 @03:23PM (#1402227)
Whoops, 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.
Re:Interviewing programmers: coding test example explained by avar on Thursday June 03, 2010 @05:26PM (#1402627)
Yeah, 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.
Re:Interviewing programmers: coding test example explained by cperciva on Thursday June 03, 2010 @06:10PM (#1402774)
Yeah, 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).

Re:Interviewing programmers: coding test example explained by nuxi on Friday June 04, 2010 @08:13AM (#1404066)
Here'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
Re:Interviewing programmers: coding test example explained by tedunangst on Thursday June 03, 2010 @11:56AM (#1401659)
The C standard exclusively uses the phrase "null-terminated" to refer to strings.
Re:Interviewing programmers: coding test example explained by cperciva on Thursday June 03, 2010 @03:15PM (#1402208)
Huh, you're right. I wonder when they changed that.
Re:Interviewing programmers: coding test example explained by nuxi on Thursday June 03, 2010 @04:18PM (#1402403)
They 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."
Re:Interviewing programmers: coding test example explained by JoachimSchipper on Thursday June 03, 2010 @06:12PM (#1402777)
I 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.

Re:Interviewing programmers: coding test example explained by nuxi on Friday June 04, 2010 @07:48AM (#1404032)
Re:Interviewing programmers: coding test example explained by JoachimSchipper on Friday June 04, 2010 @07:59AM (#1404040)
I read that as "ASCII must be supported, do whatever else you want". But I don't want to spend too much time on this...
Re:Interviewing programmers: coding test example explained by jonsen on Thursday June 03, 2010 @03:31PM (#1402257)
NUL is in ASCII an abbreviation for null.
Re:Interviewing programmers: coding test example explained by Xurinos on Thursday June 03, 2010 @10:29AM (#1401395)
As it happens, '\0' translates to 0. Since they are composed of characters, C programmers tend to do the c == '\0' check instead of the shorted c == 0 check, just to make sure everyone knows we are still treating the char like a character in a string; it is just style.

  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.

Re:Interviewing programmers: coding test example explained by bnoordhuis on Thursday June 03, 2010 @02:38PM (#1402083)
Sorry, but I can't help to nitpick: 65 is not an 'A' on systems that use EBCDIC, like z/OS and OS/400. It's not even a valid character as such, I think.
Re:Interviewing programmers: coding test example explained by infinite8s on Thursday June 03, 2010 @12:24PM (#1401728)
What people really mean is NUL terminated (NUL is the name of the '\0' character - ASCII value 0x0000). But it probably just mangled some time ago and people write it as NULL.
Re:Interviewing programmers: coding test example explained by nevinera on Thursday June 03, 2010 @11:37AM (#1401592)
>it is a bit weird to say they are null-terminated

Yeah, it would be. They're NUL-terminated.

Re:Interviewing programmers: coding test example explained by Xurinos on Thursday June 03, 2010 @11:49AM (#1401627)
Unfortunately, "null-terminated", although incorrect, is widely used. :(

http://googlefight.com/index.php?lang=en_GB&word1=%22nul...

I hate all the variations of nothing. undef, null, NULL, NIL, NUL...

Re:Interviewing programmers: coding test example explained by tedunangst on Thursday June 03, 2010 @11:57AM (#1401662)
How can "null-terminated" be incorrect when that is the language used by the C standard?
Re:Interviewing programmers: coding test example explained by Xurinos on Thursday June 03, 2010 @12:10PM (#1401697)
I stand corrected. I looked at my old ASCII tables. This is a silly semantic argument.

http://www.asciitable.com/

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.

Re:Interviewing programmers: coding test example explained by nuxi on Thursday June 03, 2010 @05:10PM (#1402573)
I'm not convinced that NUL vs. C's "null character" is just a silly semantic argument. NUL is an ASCII abbreviation and the implementors probably wanted to avoid being tied to a particular character encoding - there is no mention of NUL in the C standard.

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

Re:Interviewing programmers: coding test example explained by nevinera on Thursday June 03, 2010 @01:13PM (#1401864)
if '\0' was a legitimate member, it was not a string, it was a character/byte array, probably of fixed size.