String assignments; Was: When is a cast not a cast?

Robert Seals rds95 at leah.Albany.Edu
Wed May 3 00:06:52 AEST 1989


> In article <2747 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
> [some stuff about pointers which has been conquered with aplomb by
> Bill Davidson et al...but the stuff that gets me is...]
>
> | 3	char *c;
> ...
> | 7	c = "somestring";  /* Nothing fancy, null-terminated. */

Now I always thought this kind of thing was extremely bad form, but
I've recently seen a couple of code examples that use this. 
One figures that if ``somestring'' is generated by the compiler somewhere,
then the assignment will work as expected, because the address of it will
be copied to ``c''. But what if the compiler decides that ``somestring''
has finished it's duty to the program, and overwrites the space it took
with something else? Then ``c'' points to rubbish. Right?

I've always used stuff like ``char *c = "string";'' which the compiler
should know how to deal with, as it's an initializer, or something along
the lines of
	char *c;
	c = (char *) malloc(something);
	strcpy(c, somethingelse);

Have I been misguided all this time?

rob



More information about the Comp.lang.c mailing list