String help!
John Chambers
jc at mit-athena.UUCP
Tue Mar 12 07:46:10 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:
char fup[11]; /* I hope this isn't read-only */
int i;
...
for (i=0; i<11; i++)
fup[i] = i + '0';
fup[i] = '\0';
or maybe:
char *fup;
int i;
...
fup = malloc(11);
if (fup == NULL) { /* Gotta check for failure */
fprintf(stderr,"Out of space, can't get 11 bytes\n");
perror("foo");
exit(17);
}
for (i=0; i<11; i++) /* Initialize the sucker */
fup[i] = i + '0';
fup[i] = '\0';
To this, I say "NONSENSE". Any compiler-writer that considers a
string constant to be read-only is a total and utter turkey, and
I would rather use a sensible compiler. I don't need such stuff
to make my code slower and kludgier and harder to understand; I
can do a bad enough job by myself without encouragement from the
compiler writers! (:-)
Please, writing simple, straightforward code is a hard enough job
already. One of the nice things about C is that such things as
the above example need not be kludgy and hard to read. If we are
going to change C, let's try to make it better, not worse!
John Chambers
P.S. An extension to the language to declare a symbol to be a
constant would be nice at times. It would help those who are
dealing with ROM.
More information about the Comp.lang.c
mailing list