Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!apple!sun-barr!sun!plx!evan From: evan@plx.UUCP (Evan Bigall) Newsgroups: comp.lang.c Subject: Re: Array intialization Keywords: array of pointers Message-ID: <1960@plx.UUCP> Date: 3 Jun 89 17:32:44 GMT References: <3420@ihuxv.ATT.COM> Reply-To: evan@plx.UUCP (Evan Bigall) Organization: Plexus Computers; San Jose, CA Lines: 34 In article <3420@ihuxv.ATT.COM> bareta@ihuxv.ATT.COM (Benyukhis) writes: >The following declaration is illegal. Why?????? > >char *a = "string1"; >char *b = "string2"; >char *c = "string3"; > >char *g[] = { a, b, c }; The above declarations will fail if they are placed outside of a function (static) because statics must be initialized with constants. The value of the pointer char *a is not constant. You can make it constant by declaring it char a[] = "string1"; Which allocates 8 char's in memory and effectivly gives you a constant pointer "a" to them. Note: you will NOT be able to change the value of "a" (you WILL be able to change the contents of the 8 chars it points to). The above declarations will fail if they are placed inside a function (auto) because you can not initialize auto arrays. The following: static char *g[] = {"string1", "string2", "string3"}; Will always compile, but may or may not do what you want. Evan >What if the compiler was 3 pass one? -- Evan Bigall, Plexus Computers, San Jose, CA (408)943-2283 ...!sun!plx!evan "I barely have the authority to speak for myself, certainly not anybody else"