Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!homxb!mtuxo!mtune!codas!killer!jfh From: jfh@killer.UUCP (John Haugh) Newsgroups: comp.lang.c Subject: Re: Autoincrement question Summary: What you mean is only use one assignment per statement. Message-ID: <2464@killer.UUCP> Date: 14 Dec 87 18:27:07 GMT References: <1507@ogcvax.UUCP> <3333@sigi.Colorado.EDU> <7593@eddie.MIT.EDU> <2610@mmintl.UUCP> Organization: Big "D" Home for Wayward Hackers Lines: 52 In article <2610@mmintl.UUCP>, franka@mmintl.UUCP (Frank Adams) writes: > In article <7593@eddie.MIT.EDU> jbs@eddie.MIT.EDU (Jeff Siegal) writes: > >Summary: Don't use the target of an auto-increment or auto-decrement > >operator elsewhere in the same expression. This is correct, but not completely. Consider the other operators which return results, but aren't ++ or --. Things like, +=, -=, and so on. Even just plain '=' is a candidate. > Slightly stronger: don't use the target of an assignment operator elsewhere > in the same expression. Auto-increment and auto-decrement operators are > assignment operators. Way to strong. What you (seem) to be saying is `don't do "a = a + 1"', although I don't think anyone is going to stop doing that. The expression a += a++ + 5 is still very legal. *a += a++ + 5 is a different matter because the value of a is being used - at the same time (ie, the order of evalauation is undefined) more than once. So, even a[b++] = b; is undefined - the value is side effected and used - in the same statement. The statement (a[b + 1] = b), b++; is legit, since the ',' operator insures the left side is evaluated first, and this removes the `undefined' nature of the beast. - John. > (Actually, you can use such results, provided that the assignment and the > other use are within different arguments of a comma operator. That is, both > "f(i) , i++;" and "i++ , f(i);" are unambiguous. But don't do this unless > (a) you know exactly what you are doing, and (b) you really need to.) I don't even know what you are saying here. Unless f() is a macro, your argument is not even needed. The arguments are evaluated _prior_ to the function being called. Since you didn't pass the address of i, and assuming the address is not hanging around someplace, you can't change i from inside the function call. Given the lack of f() being a macro, f(++i) and f(i++) are both `safe'. Maybe in a different language it might be risky, but not in C. > Frank Adams ihnp4!philabs!pwa-b!mmintl!franka > Ashton-Tate 52 Oakland Ave North E. Hartford, CT 06108 - John. -- John F. Haugh II SNAIL: HECI Exploration Co. Inc. UUCP: ...!ihnp4!killer!jfh 11910 Greenville Ave, Suite 600 ...!ihnp4!killer!rpp386!jfh Dallas, TX. 75243 "Don't Have an Oil Well? Then Buy One!" (214) 231-0993