String help! (getting ANSI C strings back out of ROM)

Sam Kendall kendall at talcott.UUCP
Fri Mar 22 10:19:20 AEST 1985


> 	mktemp ("/tmp/fooXXXXXX");
> 
> 		becomes
> 
> 	mktemp ( (char *) "/tmp/fooXXXXXX");
> 
> Since the cast is used withOUT the "const" qualifier, the compiler is forced
> to place the string in writable memory.  Note that I am NOT casting a "char *"
> back into a "char *", but am instead casting a "const char *" into a "char *".
> 
> 					Tony Hansen
> 					pegasus!hansen

This won't do it.  The cast makes a pointer to non-const out of a pointer
to const, but the result of the cast still points to storage defined as const,
so the result of any attempt to modify it (by mktemp) is undefined.

See Guy Harris's note on this topic for the right way to do it.

	Sam Kendall
	Delft Consulting
	{allegra, ihnp4}!pyuxvv!delftcc!sam



More information about the Comp.lang.c mailing list