Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!mcsun!inria!irisa!engel From: engel@irisa.irisa.fr (Jean Christophe Engel) Newsgroups: comp.sys.atari.st Subject: Re: C question Message-ID: <1776@irisa.irisa.fr> Date: 5 Dec 89 10:51:36 GMT References: <8912050802.AA12717@ucbvax.Berkeley.EDU> Sender: news@irisa.irisa.fr Lines: 29 From article <8912050802.AA12717@ucbvax.Berkeley.EDU>, by S61304@PRIME-A.POLY-SOUTH-WEST.AC.UK (Rat): > Question. > > Why wont Sozobon, Lattice or any other C compiler I've tried compile the > following, from K&R? > > main() > $ > char fred[] = "Some string constant"; > > If you want to initialize an array (and a string IS an array of char) WITHIN the body of a function (whether "main" or not), you should declare it as "static" like in: static char fred[] = "Some string constant"; Thus, the array will be static, i.e. memory will be allocated once at compilation-time and initialization can be performed AT COMPILATION-time. Otherwise, i.e. if not declared static, memory will be allocated each time the function is called, and de-allocated each time the function ends, thus discarding any value the array had previously. ------------------------------------------------------------------------ | Jean-Christophe Engel (Equipe TEMIS) Phone: +33 99 36 20 00 | | IRISA Fax: 99 38 38 32 | | Campus Universitaire de Beaulieu Telex: UNIRISA 950 473F | | 35042 RENNES Cedex - FRANCE E-mail: engel@irisa.fr | ------------------------------------------------------------------------