modification of strings

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Feb 4 19:42:18 AEST 1989


In article <345 at lakesys.UUCP> chad at lakesys.UUCP (D. Chadwick Gibbons) writes:
>Insofar as I have been told, strings can not be modified ...

That depends on the implementation.  Some permit it.  However,
you cannot portably count on being able to modify a string literal.

>                char *blah = "meow";
>                char *tmp;
>                tmp = strcpy(blah, "grr, snarl, hiss");
>I would think since the string 'blah' is considered to be nonmodifiable that
>it would not be changed, but the result would be placed into tmp.

No, check the definition of strcpy().  You're attempting to modify a
string literal.  strcpy() is not obliged to second-guess your intentions
and somehow save your ass.  In fact in most implementations it isn't
able to efficiently ascertain that you're misusing it until it makes
the actual write attempt, at which point it's already too late.



More information about the Comp.lang.c mailing list