In article <44**********************@news.xs4all.nl>
Schraalhans Keukenmeester <fi********************@xsfourall.ennelwrote:
>According to www.c-faq.com (Q6.6)
int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}
[snippage]
>I see the logic why a value can be assigned to str in the function, but
would there be any point in doing so ? Am I wrong when I think the
passed array never gets the assigned value when the function returns?
You are correct about this, as others have noted.
As for why one might write f() as shown above ... well, consider
the following minor rewrite (to make "str" obviously a pointer)
with a partial expansion of the "...":
/*
* Perform various transformations. If the argument is "none"
* or "", the transformation is just a copy.
*/
int f(char *str) {
if (str[0] == '\0')
str = "none";
printf("please wait, doing <%stransformation\n", str);
... big nested loop ...
...
if (strcmp(str, "none") != 0)
... do some extra work ...
...
...
...
}
Without the assignment, this prints "doing <transformation"; with
it, it prints "doing <nonetransformation" (assuming str is an empty
string). Then, inside the nested loop, a single strcmp() against
"none" suffices. Without the assignment to "str", we might have
to write this as:
int f(char *str) {
printf("please wait, doing <%stransformation\n",
str[0] ? str : "none");
... big nested loop ...
...
if (strcmp(str, "") != 0 && strcmp(str, "none") != 0)
... do some extra work ...
...
...
...
}
(or similar). So changing the local variable "str", without changing
the caller's array, is sometimes a convenient way to reduce the
number of cases that must be handled inside the function.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.