Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Is this ok?? Message-ID: <10784@dog.ee.lbl.gov> Date: 10 Mar 91 03:49:50 GMT References: <1991Mar09.092611.24821@pilikia.pegasus.com> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Distribution: comp Organization: Lawrence Berkeley Laboratory, Berkeley CA Lines: 69 X-Local-Date: Sat, 9 Mar 91 19:49:50 PST In article <1991Mar09.092611.24821@pilikia.pegasus.com> art@pilikia.pegasus.com (Art Neilson) writes: >After seeing lots of replies to John Davis I went back and examined the >program he posted again and realized I was wrong regarding his pointer >usage, I sorta jumped the gun when I saw the &'s and ** stuff, I was >wrong. Indeed. (Incidentally, the reason his program `failed' on VMS was that he was testing a different program, an easy enough mistake to make; when he tested the one he posted, it worked.) >I still don't think it's OK to assign the quoted string "Hello\n" >to *s in fm2() as shown below. Where does *s point to ? Where in storage >would "Hello\n" reside ? Does the compiler assign some scratch storage or >something for it ?? If you do not know the answer to the latter question, you should not be posting definitive followups to comp.lang.c. A double quoted string in C is, with one exception%, an anonymous object of type `array N of char', where N is one more than the number of characters enclosed in quotes (after escape interpolation). The value of an array object is of course determined by The Rule, hence in >>#include >>void fm2(s) >>char **s; >>{ >> *s = "Hello\n"; >>} the object at `*s' (which will have to be an for the assignment to succeed) is made to point to the first character of this anonymous array of (in this case) 7 characters. That array itself resides in some sort of system-allocated space which has static storage duration; a typical system puts it in a code or initialized data segment. (There are atypical systems; a C compiler for Xerox D-machines once kept the original strings in Mesa `string descriptor' format and allocated the C versions on the heap. Perhaps it still does, although Xerox D-machines have largely been relegated to the Dustbin of History....) In the original example in question, `s' happened in this case to point to a single `char *' object declared in fm1(): fm1(ss) char **ss; { char *s; fm2(&s); *ss = s; } (I may have some of the original names mixed up), so the clause `which will have to be an for the assignment to succeed' is satisfied. The object at fm2's `*s' is fm1's `s', so s is made to point to the `H' in `Hello\n\0'. Fm1 then sets the object at `*ss' (which has the exact same restriction) to point to that `H' as well, and fm1's *ss in the original example was main's `s', so everything was fine, if overly convoluted. ----- % The exception occurs when a double quoted string is used as an initializer for an object of type `array N of char' or `array N of const char', including when the size N is given as `the size of the initializer': char xyz[] = "abcd"; makes xyz an , and need not leave a copy of the sequence `abcd\0' anywhere else. (If it does leave a copy somewhere else, the compiler is wasting space, unless this copy exists for, e.g., debugging purposes.) -- In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427) Berkeley, CA Domain: torek@ee.lbl.gov