Message-ID: <39BF05CB.4B98E31B@eton.powernet.co.uk> Date: Wed, 13 Sep 2000 05:42:51 +0100 From: Richard Heathfield Organization: Eton Computer Systems Ltd X-Mailer: Mozilla 4.6 [en-gb]C-CCK-MCD NetscapeOnline.co.uk (WinNT; I) X-Accept-Language: en-GB,en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp,comp.lang.c Subject: Re: Warning : if (x=y) References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 195.60.5.92 X-Trace: 13 Sep 2000 05:46:09 +0100, 195.60.5.92 Lines: 94 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Damian Yerrick wrote: > > On Wed, 13 Sep 2000 00:48:21 +0100, Jason Green > wrote: > > >Andris Pavenis 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 at the top */ memcpy(s1, s2, sizeof s2); /* legal, again if you include - 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)