Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Literal strings in C Message-ID: <562@haddock.UUCP> Date: Mon, 15-Jun-87 16:52:27 EDT Article-I.D.: haddock.562 Posted: Mon Jun 15 16:52:27 1987 Date-Received: Sun, 21-Jun-87 02:49:18 EDT References: <212@inco.UUCP> <4257@caip.rutgers.edu> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Organization: Interactive Systems, Boston Lines: 34 In article <4257@caip.rutgers.edu> brisco@caip.rutgers.edu (Thomas Paul Brisco) writes: >[mack@inco.UUCP (Dave Mack) writes:] >>c = "literal string"[i]; >>"literal string"[i] = c; >> >>The first form is not unreasonable (saves a character pointer, anyway.) >>The second statement seems utterly useless. > >The first form tends to be more than useful, it saves you not only the char*, >but in the case of a series of string constants can be downright useful; such >as: #define TTYS "/dev/ttya\0/dev/ttyb" >... Although I've (personally) used the second form as following > #define SCCDEV "/dev/scc?" > SCCDEV[strlen(SCCDEV) - 1] = inputdev; >it should be noted as "non-portable" (for all that's worth). This last usage is certainly dangerous, since a compiler may (and in a strict pre-ANSI implementation, must) store each instance of the string literal in a different location. More importantly, it isn't necessary -- even for efficiency reasons. Dave and Thomas (and others, I think) have stated that using the string literal "saves a pointer". This is true if your alternative is to write char *SCCDEV = "/dev/scc?"; but the best way to write this is simply static char SCCDEV[] = "/dev/scc?"; which should be identical to the "#define" version, except that it forces the strings to occupy the same storage (and is thus *more* efficient). The only "waste" is in the symbol table. The version with the embedded \0 can also be written this way. (I'll remain silent on the issue of whether it should be written at all.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint