Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!bobmon From: bobmon@iuvax.cs.indiana.edu (RAMontante) Newsgroups: comp.lang.c Subject: char *Foo VS. char Bar[] Message-ID: <19636@iuvax.cs.indiana.edu> Date: 14 Apr 89 03:56:37 GMT Reply-To: bobmon@iuvax.cs.indiana.edu (RAMontante) Organization: malkaryotic Lines: 32 A student turned up a curious problem today; before I tell the class what I think the problem was I'd like some confirmation from the gurus here (or denial, if you must :-( ) The essence of his code was char *foo = " "; ... sprintf(foo,"xxxx"); Using gcc on a VAX8650/Ultrix, the sprintf causes a bus error. The standard cc compiler produced code that "worked". I had him change the declaration to char foo[6] = " "; /* assume I got the lengths correct */ and the program ran correctly as compiled by gcc. My interpretation is that, in the original case, gcc has created a (variable) pointer which is initialized to some *constant* storage which can't be written to; cc doesn't make any writeable/read-only distinctions. The second form created a (constant?) pointer to some storage that is explicitly writeable, and happens to be filled with blanks initially. (BTW: He says that the original form worked under MSC under MSDOS, so I infer that MSDOS/80x86 systems also don't enforce constant strings.) All comments will be appreciated. Thanx, Bob -- Those who do not understand MSDOS are | Bob Montante (bobmon@cs.indiana.edu) condemned to write glowingly of it in | Computer Science Department slick, short-lived magazines. | Indiana University, Bloomington IN