String help!

Jeff Lichtman jeff at rtech.ARPA
Thu Mar 14 17:55:29 AEST 1985


> Hmmm... According to some of the advice here, the following is not
> an acceptable way to declare an initialized array:
> 
> 	char *fup = "0123456789";
> 
> The reason is that some C compilers are likely to take the string 
> constant and put it into a read-only portion of memory.  Instead,
> if we want an initialized character array, we are supposed to say 
> something like:
> 
	Unnecessarily complicated code here.
>
> or maybe:
> 
	Even more complicated code here.
>
> 			John Chambers

First of all, it's fine to initialize a character pointer to point to a
string constant.  Just don't try to alter the constant.  If you
want to initialize a character pointer to point to a non-constant string,
try the following:

	# define CONST	"0123456789"

	char	nonconst[sizeof(CONST)];
	char	*p = nonconst;

	strcpy(nonconst, CONST);

There, that's not so hard, is it?  I don't think it's a good programming
practice to modify the value of a constant, anyway.
-- 
Jeff Lichtman at rtech (Relational Technology, Inc.)
aka Swazoo Koolak



More information about the Comp.lang.c mailing list