Date: Mon, 13 Oct 1997 12:24:19 -0400 (EDT) From: "Art S. Kagel" Reply-To: kagel AT ns1 DOT bloomberg DOT com To: Guan Foo Wah Cc: djgpp AT delorie DOT com Subject: Re: Is this a bug ??? or feature ?? In-Reply-To: <19971013092045.160.rocketmail@send1.rocketmail.com> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Mon, 13 Oct 1997, Guan Foo Wah wrote: > Check the program out below > > > //starts here------------------------------------------------------- > #include > > char *num[] = { > "one", //line 4 > "one", //line 5 :- this string exactly the same as line 4 > "three", > "four" > }; > > char *id[] = { > "ten", > "two", > "one", //line 13 :- exactly same as line 4 and 5 > "F16", > "DJGPP" > }; > > void main (void) > { > //prints the address of the 3 variables > printf ("Address : %p %p %p %p\n", num[0], num[1], id[2]); > > //prints the value of the 3 variables before modifying > printf ("Value before modifying : %s %s %s\n", num[0], num[1], > id[2]); > > //modify one of the 3 variables > num[0][0] = 'f'; > > //prints the value of the 3 variables after modifying > printf ("Value after modifying : %s %s %s\n", num[0], num[1], > id[2]); > } > > //ends here----------------------------------------------------------- Your results are as one would expect and simply show the difference between two implementations. GCC places all literal strings together in memory and only creates a particular string once. By assigning the same literal string to several element of array of char you get the same address as the string only exists once. You then modify the literal which changes it in memory and all of the array elements pointing to it change also. Borland simply does not perform this space optimization so the results differ. First you should not be modifying literal strings, it is bad form and bad code. If the strings may me modified then malloc space for the elements of the array to point to and strcpy() the literals into that space. Art S. Kagel, kagel AT bloomberg DOT com