Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!mcnc!rti!xyzzy!throopw From: throopw@xyzzy.UUCP (Wayne A. Throop) Newsgroups: comp.lang.c Subject: Re: Style [++i vs i++] Message-ID: <189@xyzzy.UUCP> Date: Sun, 9-Aug-87 18:10:45 EDT Article-I.D.: xyzzy.189 Posted: Sun Aug 9 18:10:45 1987 Date-Received: Sun, 9-Aug-87 23:06:57 EDT References: <8298@brl-adm.ARPA> <587@cblpe.ATT.COM> Organization: Data General, RTP NC. Lines: 55 > apc@cblpe.ATT.COM (Alan Curtis) > Does this scare anyone (does any else's compiler do the same thing?) > main(a) > char (*a)[]; > { a = 0; printf("a=0x%x\n", a); a++; printf("a=0x%x\n", a); } > produces: > a=0x0 > a=0x0 It doesn't scare me. It perhaps causes me a momentary fright that anybody thought that that should do something sensible, but then I'm used to such things by now. > I realize that is the logical extension of a++ somtimes > adding 8,4,23.5, whathavyou, to a, but it still scares me. > PS: In my book a++ should add 1 (one, uno, etc). > If I wanted to add sizeof(*a) Ida SAID > a += sizeof(*a); What scares me is that a++ *does* always add 1. Never 8, 4, 23.5, or anything else. Always 1. It adds one byte, or one word, or one *whatever* to the address in question. It would be illogical, immoral, byte-chauvanist, and all kinds of perjorative things for it to add one byte to the address of anything but a byte. Granted, most people are under the delusion that all C address types are byte granular offsets into per-process monolithic address segments. But then, C implementors are not bound to abide by the delusions of the masses, thank goodness. But the scariest thing about all this is that *none* *of* *my* *tools* *caught* *this* *bug*!!!! Lint happily passed the program, as did other typecheckers. And the compiler didn't complain (though on our system the output is a=0x0 a=0x1 instead, (but then the behavior is undefined since the program is illegal in the first place, so I can't blame the compiler too harshly... though I *CAN* blame lint and other type-and-usage checkers for falling down on the job). (By the way, for those of you who missed it, the program is illegal for the obvious reason that it increments a pointer to an object of unknown size, but *also* because it performs arithmetic on a null pointer, and of course, this is illegal.) -- At once words appeared on the screen: "Hello. My name is Hank Thoro II. Please type your name." "My whole name?" he typed. "Good. Do you like baseball? Just type Y for Yes or N for No." --- from "Roderick" by John Sladek -- Wayne Throop !mcnc!rti!xyzzy!throopw