Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watdaisy.UUCP Path: utzoo!watmath!watdaisy!ndiamond From: ndiamond@watdaisy.UUCP (Norman Diamond) Newsgroups: net.lang.c Subject: Re: String help! Message-ID: <7064@watdaisy.UUCP> Date: Tue, 12-Mar-85 23:23:40 EST Article-I.D.: watdaisy.7064 Posted: Tue Mar 12 23:23:40 1985 Date-Received: Wed, 13-Mar-85 01:28:15 EST References: <1156@ukma.UUCP> <113@mit-athena.UUCP> Organization: U of Waterloo, Ontario Lines: 63 > 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. fup is not read-only. The string might be. > 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 */ The array fup is not read-only. > int i; > ... > for (i=0; i<11; i++) > fup[i] = i + '0'; > fup[i] = '\0'; > > or maybe: > > char *fup; > int i; Yup, this time you gotta do it. > ... > fup = malloc(11); ... proving that fup is not read-only ... > 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". It is nonsense, all right. Try: char fup[11] = "0123456789"; This is not the same as assigning a pointer to address a read-only item. This initializes the contents of the writable array fup. -- Norman Diamond UUCP: {decvax|utzoo|ihnp4|allegra}!watmath!watdaisy!ndiamond CSNET: ndiamond%watdaisy@waterloo.csnet ARPA: ndiamond%watdaisy%waterloo.csnet@csnet-relay.arpa "Opinions are those of the keyboard, and do not reflect on me or higher-ups."