Path: utzoo!utgpu!cunews!mitel!melair!dataco!amodeo From: amodeo@dataco.UUCP (Roy Amodeo) Newsgroups: comp.lang.c Subject: Literal Strings in C ( was Re: example of how toupper() works ) Message-ID: <256@dcsun21.dataco.UUCP> Date: 18 Oct 90 04:54:16 GMT References: <2466@ux.acs.umn.edu> Reply-To: amodeo@dcsun03.UUCP (Roy Amodeo,DC ) Organization: Canadian Marconi Company (Datacomm), Ottawa, Ontario Lines: 63 In article <2466@ux.acs.umn.edu> edh@ux.acs.umn.edu (Eric D. Hendrickson) writes: >Basically, what I want to do is take a string of upper/lower case, and make >it all upper case. Here is a first try at it, > >#include >main() >{ > char *duh = "Hello"; .... > if (islower(*duh)) *duh = toupper(*duh); .... In the above segment of code, the literal string pointed to by 'duh' is being modified in place. Is this portable according to the ANSI standard? For our embedded system, we've asked the nice cross-compiler to put the literal strings with the code and the const data because literal strings are rarely modified. Since our code, const, and string area resides in a memory location where writing is verboten in user state, any user program that attempts to modify a literal string on our system will be shot for trespassing. The above program would compile, but not run. To make it run, the routine would actually have to copy the string being upcased into a buffer: char* from = "Hello"; char buf[ sizeof( "Hello" ) ]; char* to = buf; for( ; *from; from += 1, to += 1 ) if ( islower( *from ) ) *to = toupper( *from ); else *to = *from; *to = '\0'; ( Apologies if my coding style is offensive. It's designed to compensate for my marginal observational skills. ) Another reason to not modify literal strings is that the compiler may be smart enough to collapse identical literal strings: char* s1 = "hello"; char* s2 = "hello"; In this case, s1 and s2 could have identical values. If literal strings are modifiable, this space optimization is a bad idea. ( In practice, it doesn't seem to gain a lot of space anyway, so I wouldn't be surprised if most compilers don't. However I seem to remember a UNIX utility that you could run on a program to coalesce identical literal strings in this fashion if you wanted this optimization. ) What does current practice dictate on this? > Eric Hendrickson >-- >/----------"Oh carrots are divine, you get a dozen for dime, its maaaagic."-- >|Eric (the "Mentat-Philosopher") Hendrickson Academic Computing Services >|edh@ux.acs.umn.edu The game is afoot! University of Minnesota >\-"What does 'masochist' and 'amnesia' mean? Beats me, I don't remember."-- rba iv - signatures? We don't need no stinkin' signatures! amodeo@dataco