Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!hammer!jockc From: jockc@hammer.idsila.com (PRIV Account) Newsgroups: comp.lang.c Subject: Re: Is this ok?? Message-ID: Date: 11 Mar 91 22:31:05 GMT References: <1991Mar08.191107.23161@pilikia.pegasus.com> <1991Mar9.073231.1364@athena.mit.edu> <1991Mar10.040429.29309@pilikia.pegasus.com> Sender: news@hammer.UUCP Distribution: comp Organization: Int'l Digital Scientific, Inc. Lines: 61 In-reply-to: art@pilikia.pegasus.com's message of 10 Mar 91 04:04:29 GMT In article <1991Mar10.040429.29309@pilikia.pegasus.com> art@pilikia.pegasus.com (Art Neilson) writes: > ..discussion deleted.. >Guess I deserve a bit of public chastisement for my criticisms. >I still don't get why the string assignment > > *s = "Hello\n"; > >in fm2() is ok. Raymond Chen sent me an email stating that storage for >"Hello\n" was allocated as static anonymous readonly by the compiler. >I had always thought that the rvalue in a pointer assignment had to be an >address. The usual way I do assignments of this nature is to either >explicitly declare an array large enough to hold the string or malloc Think of it like this: main() { int x=1; foo(x); printf("%d\n",x); /*prints 1*/ bar(&x); printf("%d\n",x); /*prints 5*/ return 0; } foo(val) int val; { val=5; } bar(val) int *val; { *val=5; } For a function to modify the varable I pass it, I have to pass it's address. The same goes for a char pointer. I must pass the address so that the called function can change its contents: main() { char *s="some chars"; foo(s); printf("%s\n",s); /* prints "some chars" */ bar(&s); printf("%s\n",s); /* prints "different chars" */ } foo(str) char *str; { str="different chars"; /* this changes this function's copy of the passed variable's value */ } bar(str) char **str; { *str = "different chars"; }