Explain this error to me! Thanks

I have a short program below:
void main()
{
....
}
int test(char str[100])
{
char str1[100];
str = "NEWYORK";// OK----> why don't have error???
str1 = "PARIS"; // Error---> i see
...
...
}
please help me, why "str = "NEWYORK" is OK? i don't understand. according to me, str and str1 are the same. Explain this error to me! Thanks
The reason why you can assign a string literal to str should be because in C you can't pass array arguments to functions: they are always implicitly converted to pointers to the first element. Because of this, although str is defined as an array, it is really just a pointer to char holding the address of the string you specified from the caller, so you can change its value inside test. On the contrary, str1 is an array and not a pointer so you can't use the = operator to perform an array assignment.
Inside test, consider str as being of type char *. A good book (e.g. K&R) should explain this issue clearly.

Hope I have been useful.
Last edited on
Topic archived. No new replies allowed.