Mail Archives: djgpp/2000/09/13/01:03:25
Damian Yerrick wrote:
>
> On Wed, 13 Sep 2000 00:48:21 +0100, Jason Green
> <news AT jgreen4 DOT fsnet DOT co DOT uk> wrote:
>
> >Andris Pavenis <pavenis AT lanet DOT lv> wrote:
> >
> >> With gcc-2.95.2 -fpedantic-errors is default for C++ ...
> >
> >Unless you compile the following as C++ code:
> >
> >int main(void)
> >{
> > char s1[4], s2[4] = "foo";
> >
> > s1 = s2; /* illegal */
> >
> > return 0;
> >}
> >
> >I don't know if this is bug or feature [in GCC].
>
> Feature. AFAIK an array name is not an lvalue. You can, OTOH, say
>
> char *s1;
> char *s2[4] = "bar";
> s1 = s2;
>
> comp.lang.c added for a full explanation of this language issue.
Sure.
int main(void)
{
char s1[4], s2[4] = "foo";
s1 = s2; /* illegal */
strcpy(s1, s2); /* legal if you #include <string.h> at the top */
memcpy(s1, s2, sizeof s2); /* legal, again if you include <string.h> -
care with that third parameter, it's the number of bytes to copy */
return 0;
}
Now, why is s1 = s2 illegal? After all, an array name *is* an lvalue.
But it's /not/ a *modifiable lvalue*. Here's what the current ISO C
Standard has to say on the matter:
6.3.2 Other operands
6.3.2.1 Lvalues, arrays, and function designators
1 Anlvalue is an expression with an object type or an incomplete type
other than void;53)
if an lvalue does not designate an object when it is evaluated, the
behavior is undefined.
When an object is said to have a particular type, the type is specified
by the lvalue used to
designate the object. A modifiable lvalue is an lvalue that does not
have array type, does
not have an incomplete type, does not have a const-qualified type, and
if it is a structure
or union, does not have any member (including, recursively, any member
or element of
all contained aggregates or unions) with a const-qualified type.
In other words, you can initialise, as for s2 here:
char s1[4], s2[4] = "foo";
but you can't assign, as shown by the illegal assignment to s1 here:
s1 = s2;
Now, the next program fragment says:
char *s1;
char *s2[4] = "bar";
s1 = s2;
This is almost legal. I think there's a tismype on the middle line. If
you meant:
char s2[4] = "bar";
then yes, you can point s1 at it quite happily, because s1 is now a
pointer, not an array.
HTH. HAND.
--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
65 K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html (32
to go)
- Raw text -