Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mcvax!ukc!eagle!icdoc!dcw From: dcw@doc.ic.ac.uk (Duncan C White) Newsgroups: comp.lang.c Subject: Re: Silly program [was Re: Style [++i vs i++] ] Message-ID: <513@ivax.doc.ic.ac.uk> Date: Tue, 11-Aug-87 08:05:47 EDT Article-I.D.: ivax.513 Posted: Tue Aug 11 08:05:47 1987 Date-Received: Fri, 14-Aug-87 01:44:59 EDT References: <8298@brl-adm.ARPA> <587@cblpe.ATT.COM> <189@xyzzy.UUCP> Reply-To: dcw@doc.ic.ac.uk (Duncan C White) Organization: Dept. of Computing, Imperial College, London, UK. Lines: 99 In article <587@cblpe.ATT.COM> apc@cblpe.ATT.COM (Alan Curtis) writes: > 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 The first thing which really scares me about this program is that the first parameter to main is ARGC - an integer ! Declaring it as 'char (*a)[]' seems highly suspicious.... This program appears to be using a parameter as a yukky way of declaring the variable: the problem can be simplified by declaring 'a' as as a local variable inside main: main() { char (*a)[]; a = 0; printf("a=0x%x\n", a); a++; printf("a=0x%x\n", a); } In article <189@xyzzy.UUCP> throopw@xyzzy.UUCP (Wayne A. Throop) writes: > >....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. > Yes, Wayne has hit the nail on the head here: a is declared as a pointer to an indefinite sized string. Logically, there's no such thing as an increment operation on it.. However, Wayne also quoted Alan : >> 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); and went on to say: > >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. Agreed, C adds one "unit" according to the type.. But I don't quite see why that scares Wayne : It seems like the most convenient behaviour to me.. one of the commonest cases is where I have an array of elements of a type t, and am sequentially scanning through the array elements using a pointer. The increment operation for such a pointer is surely 'move onto the next element' rather than 'move on one byte' ?? It would be extremely inconvient if I had to always use 'p += sizeof(t)' > ... 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. Agreed... I think Alan is wrong here: if he really wants to add one byte, [well, one char] to something that is a 'pointer to an element of type t' then I guess he should use: p = (int *) ( 1 + (char *) p ); Wayne finishes off: > >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.... > Yes - this is very worrying.. any lint gurus out there care to explain why this is not detected ? Duncan. ----------------------------------------------------------------------------- JANET address : dcw@uk.ac.ic.doc| Snail Mail : Duncan White, --------------------------------| Dept of Computing, This space intentionally | Imperial College, left blank...... | 180 Queen's Gate, (paradoxical excerpt from | South Kensington, IBM manuals) | London SW7 ---------------------------------------------------------------------------- Tel: UK 01-589-5111 x 4982/4991 ----------------------------------------------------------------------------