Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utfyzx!sq!msb From: msb@sq.UUCP Newsgroups: comp.lang.c Subject: Re: char (*a)[] (was: Style [++i vs i++]) Message-ID: <1987Aug10.192923.7879@sq.uucp> Date: Mon, 10-Aug-87 19:29:23 EDT Article-I.D.: sq.1987Aug10.192923.7879 Posted: Mon Aug 10 19:29:23 1987 Date-Received: Tue, 11-Aug-87 06:25:23 EDT References: <8298@brl-adm.ARPA> <587@cblpe.ATT.COM> <189@xyzzy.UUCP> Reply-To: msb@sq.UUCP (Mark Brader) Organization: SoftQuad Inc., Toronto Lines: 46 Checksum: 42277 Regarding the code... > > main(a) > > char (*a)[]; > > { a = 0; printf("a=0x%x\n", a); a++; printf("a=0x%x\n", a); } Wayne Throop writes: > But the scariest thing about all this is that *none* *of* *my* *tools* > *caught* *this* *bug*!!!! Lint happily passed the program ... > And the compiler didn't complain ... Same on our machine, by the way. > (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, Actually, *declaring* such a pointer is probably illegal. The language in K&R appendix A section 8.4 is a bit fuzzy, but seems to imply this; and section 3.5.3.2 of the (Oct.'86) ANSI draft nails it down clearly. > but *also* because it performs arithmetic on a null pointer, and > of course, this is illegal.) Um, I don't think so, Wayne; it's just that the result, if you indirect through such a pointer, is undefined. K&R is silent on this, but ANSI 3.3.6 seems pretty clear. And here the pointer isn't being indirected through. The OTHER thing that's wrong with the code is that a "%x" format is used to print a pointer variable. "%x" is used to print ints, or at least, things that printf() can pretend are ints. Pointers needn't be the same size as ints. It's much safer to do this: printf ("a=0x%lx\n", (long) a); Then you get surprised only if the pointers won't even fit in a long. ANSI has a better solution to this: the new format "%p". (See 4.9.6.1). On an ANSI compiler, you would write: printf ("a=%p\n", (void *) a); and be guaranteed reasonable results. But I don't think "%p" exists yet. Mark Brader, utzoo!sq!msb C unions never strike!