S
Schraalhans Keukenmeester
According to www.c-faq.com (Q6.6)
--------
Q: If you can't assign to arrays, then how can
int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}
work?
A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.
----------
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?
Or am I missing something here ? (Tried the example in C99 mode as well
as C89) mystr[]'s value has not changed after:
char mystr[10];
mystr[0]='\0';
f(mystr);
printf("%s\n",mystr);
I have sofar always used strcpy in cases like this, but now I am in
doubt whether I overlooked something...
Thanks in advance.
Sh
--------
Q: If you can't assign to arrays, then how can
int f(char str[])
{
if(str[0] == '\0')
str = "none";
...
}
work?
A: In this code, str is a function parameter, so its declaration is
rewritten by the compiler as described in question 6.4. In other words,
str is a pointer (of type char *), and it is legal to assign to it.
----------
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?
Or am I missing something here ? (Tried the example in C99 mode as well
as C89) mystr[]'s value has not changed after:
char mystr[10];
mystr[0]='\0';
f(mystr);
printf("%s\n",mystr);
I have sofar always used strcpy in cases like this, but now I am in
doubt whether I overlooked something...
Thanks in advance.
Sh