Is this ok??
PRIV Account
jockc at hammer.idsila.com
Tue Mar 12 08:31:05 AEST 1991
In article <1991Mar10.040429.29309 at pilikia.pegasus.com> art at 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";
}
More information about the Comp.lang.c
mailing list