char *Foo VS. char Bar[]

RAMontante bobmon at iuvax.cs.indiana.edu
Fri Apr 14 13:56:37 AEST 1989


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 at cs.indiana.edu)
condemned to write glowingly of it in  | Computer Science Department
slick, short-lived magazines.          | Indiana University, Bloomington IN



More information about the Comp.lang.c mailing list