Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mit-athena.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!mit-athena!jc From: jc@mit-athena.UUCP (John Chambers) Newsgroups: net.lang.c Subject: Re: String help! Message-ID: <113@mit-athena.UUCP> Date: Mon, 11-Mar-85 16:46:10 EST Article-I.D.: mit-athe.113 Posted: Mon Mar 11 16:46:10 1985 Date-Received: Wed, 13-Mar-85 01:17:30 EST References: <1156@ukma.UUCP> Organization: MIT Project Athena Lines: 49 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.