String help!
Guy Harris
guy at rlgvax.UUCP
Wed Mar 13 18:19:41 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";
Damn straight. It defines an initialized *pointer* which points to
a nameless array. Try
char fup[] = "0123456789";
to declare an array.
This *is* the sensible thing to do. If you actually plan to *modify*
that array, the first example is truly lousy; if you end up putting 11
characters into it, you stomp on some strange area of memory (unless
the string is put in read-only memory, in which case your program gets
properly punished). If you expect up to 128 characters in the array,
try
char fup[128+1/*for the null terminator*/] = "0123456789";
Why should random strings get put in writable areas of your address space?
Currently, there are a lot of horrible kludges to move strings, etc. into
read-only sharable text space; putting strings there by default obviates
the need for some of those kludges (the "const" attribute obviates the
need for the others).
Please learn how to construct initialized strings before blindly
criticizing this language change.
Guy Harris
{seismo,ihnp4,allegra}!rlgvax!guy
--
Guy Harris
{seismo,ihnp4,allegra}!rlgvax!guy
More information about the Comp.lang.c
mailing list