Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!snorkelwacker.mit.edu!hsdndev!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.lang.c Subject: Re: Is this ok?? Message-ID: <24668:Mar921:07:4791@kramden.acf.nyu.edu> Date: 9 Mar 91 21:07:47 GMT References: <1991Mar09.092611.24821@pilikia.pegasus.com> Organization: IR Lines: 32 In article <1991Mar09.092611.24821@pilikia.pegasus.com> art@pilikia.pegasus.com (Art Neilson) writes: > 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 ?? [ void fm2(s) char **s; { *s = "Hello\n"; } ] "Hello\n" is a constant string. When the compiler sees it, it makes room for it somewhere, and replaces "Hello\n" by a pointer to that location. Under UNIX, for example, the string is either stored along with the unwritable process text, or in the initialized data region, depending on your compiler. Now the string "Hello\n" has value if 0x3753 is the location where the compiler put "Hello\n". In fm2, s has type . More precisely, say s has value . This means that at location 0x87654 there's a , say . The assignment *s = "Hello\n" means ``Take s's value (a location in memory), and store a pointer to "Hello\n" in that location.'' In this case, that means to store at the location . The old value of *s, namely 0x14702, is replaced by a pointer to "Hello\n", namely 0x3753. So the string "Hello\n" is never copied. ``*s'' refers to a pointer-to-char object, and ``"Hello\n"'' has a pointer-to-char value. The assignment just stores the value inside the object. Chris will undoubtedly give a more comprehensible explanation. ---Dan