modification of strings

Karl Heuer karl at haddock.ima.isc.com
Thu Feb 9 06:39:24 AEST 1989


In article <466 at oglvee.UUCP> norm at oglvee.UUCP (Norman Joseph) writes:
>[The previous example overflows,] but what if the code looked like this:
>                char *blah = "meow\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
>                strcpy( blah, "grr, snarl, hiss" );
>assuming that you could write to the space into which `blah' pointed?

You'd better also assume that string literals are not shared.  Even so, you
may be in for a surprise when you execute this code fragment the second time,
and find that blah[0]=='g' immediately after the initialization to
(apparently) "meow".

This kludge is confusing and unportable.  Don't use it.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list