Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!husc6!hao!ames!oliveb!intelca!mipos3!omepd!intelisc!littlei!ogcvax!schaefer From: schaefer@ogcvax.UUCP Newsgroups: comp.lang.c Subject: Autoincrement question Message-ID: <1507@ogcvax.UUCP> Date: Thu, 3-Dec-87 16:14:11 EST Article-I.D.: ogcvax.1507 Posted: Thu Dec 3 16:14:11 1987 Date-Received: Thu, 10-Dec-87 02:31:58 EST Reply-To: schaefer@ogcvax.UUCP (Barton E. Schaefer) Organization: Oregon Graduate Center, Beaverton, OR Lines: 50 (I realize this might be similar to another question asked recently, but ...) Another student here at OGC recently came to me with a question about the C autoincrement operator. The following program is representative of the code he wrote, which did not do what he expected: struct foo { struct foo *tmp; char junk[32]; } foolist[4]; main () { struct foo *bar; bar = foolist; /* Do something with bar */ bar->tmp = bar++; /* This is the problem line */ /* Do something else */ } This was compiled with the 4.3 BSD UNIX C compiler (not ANSI conformant). What he really wanted was the equivalent of bar->tmp = bar; bar++; This program DOES do what he expected the above to accomplish: main () { struct foo *bar; bar = foolist; /* Do something with bar */ (bar++)->tmp = bar; /* Do something else */ } I know HOW the results of the two programs differ (I looked at the assembly code) but I was wondering if someone could explain WHY they differ. Obviously, when bar++ appears on the right side of the assignment, the old value of bar is saved, then the increment is computed, and lastly bar->tmp is evaluated and the assignment done. The real question is: Is the order of evaluation in a statement like bar->tmp = bar++; well-defined, or is it implementation-dependent? And if it is well-defined, WHERE is it defined? Reference please. -- Bart Schaefer CSNET: schaefer@cse.ogc.edu UUCP: ...{tektronix,verdix}!ogcvax!schaefer "A band of BIG, DUMB, LOUDMOUTHED, BUNGLING OGRES is a GREAT ASSET to the neighbohood. It keeps out the RIFF-RAFF." -- Wormy