Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!uw-june!uw-entropy!quick!srg From: srg@quick.COM (Spencer Garrett) Newsgroups: comp.arch Subject: Null-terminated C strings Message-ID: <174@quick.COM> Date: 21 Dec 87 00:09:29 GMT References: <261@ivory.SanDiego.NCR.COM> <164@sdeggo.UUCP> Organization: Quicksilver Engineering, Seattle Lines: 36 In article <164@sdeggo.UUCP>, dave@sdeggo.UUCP (David L. Smith) writes: > In article <261@ivory.SanDiego.NCR.COM>, jan@ivory.SanDiego.NCR.COM (Jan Stubbs) writes: > > Personally, I can't imagine any convenience a null terminated string would > > have over a string preceded by its length. > > Well, there is no limit imposed on the length of the string. It also takes > up one less byte per string in overhead (unless you wanted to limit your > string length to 255 characters) which isn't very important today, but > probably was when C was first defined. Ahem. What makes you think 2 bytes would be enough? The VAX is constantly tripping over its own feet because the designers couldn't bring themselves to make ALL fields big enough to describe anything architecturally possible. (e.g. - 16-bit conditional branch offsets and string lengths) I can think of two major advantages of null-terminated strings over strings preceded by their lengths. 1) You can pass substrings around without copying or altering the original string. (String tails, at least, but that's usually what you want. "process some part of the string and pass the rest on") This is why PL/I used separate "dope vectors" to describe strings, but this is a level of complexity completely inappropriate as part of the C language. You can write a library to do that in C. (Try writing the inverse library in Pascal!) 2) Having a CHARACTER to mark the end of a string is ever so much more convenient and efficient than having to compare lengths all the time (assuming you're looking at the characters and not just copying them, and even that is an implementation issue and not truly fundamental). The general paradigm is: while (the next character is within the interesting range) do something interesting with it; now look at what the uninteresting character was;