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

Tony L. Hansen hansen at pegasus.UUCP
Tue Mar 19 14:29:44 AEST 1985


By definition, in ANSI C (according to the latest Feb. draft) string
constants are of type "const char[]".  The first horrified thought that
comes to the mind of many UNIX programmers is "What do I do with all of my
calls to mktemp() now?"  Easy!

	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 *".

Since the number of times that such idioms will be needed will be very small,
this change to ANSI C will definitely be more beneficial than harmful.

					Tony Hansen
					pegasus!hansen



More information about the Comp.lang.c mailing list