Given a pointer and an array, setting one equal to another fails in one case, and works in another.
char *c_ptr = "I'm a char pointer";
char c_arry[] = "I'm a char array";
c_ptr = c_arry; //This line works.
c_arry = c_ptr; //This line fails.
When the line above fails, it generates this warning: "error: incompatible types when assigning to type". This error seems disingenuous, and frankly, a little slutty, given that just a line prior, she claimed she was my type.
Trying to understand this issue, I re-read K&R's chapter on pointers and arrays and noticed on page 102 their claim that "the array name is the address of the zeroth element". If K&R is accurate, substituting in &c_arry[0] should generate the same error, right? But this line:
&c_arry[0] = c_ptr;
gives me a new error: "lvalue required as left operand of assignment". Why does this so-called identical value generate a different error? When GCC and K&R contradict each another, that's when I turn to the experts on stackoverflow.
Reading up a bit on l-values, I've come to the conclusion that this is an L-value vs. R-value issue, and that c_arry can only be an L-value - that is, an assigned value - in two, and only two situations:
- String initialization (i.e.
char c_arry[] = "I'm a char array";) - and character assignment (i.e.
c_arry[0] = 'A';)
Is my conclusion accurate? Are these the only two ways that c_arry can ever be assigned?
Or put another way, is there ever an X, for any of the following lines of code, that is ever valid?
c_arry = X;
&c_arry = X;
&c_arry[0] = X;
*c_arry = X;
*c_arry[0] = X;