Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!topaz!ll-xn!cit-vax!nike!think!ima!haddock!karl From: karl@haddock Newsgroups: net.lang.c Subject: Re: uses of void Message-ID: <86900012@haddock> Date: Fri, 15-Aug-86 17:07:00 EDT Article-I.D.: haddock.86900012 Posted: Fri Aug 15 17:07:00 1986 Date-Received: Sun, 17-Aug-86 07:28:33 EDT References: <277@sdchema.sdchem.UUCP> Lines: 22 Nf-ID: #R:sdchema.sdchem.UUCP:277:haddock:86900012:000:883 Nf-From: haddock!karl Aug 15 17:07:00 1986 sdchem!tps writes: >sort( (void *)&foo[0], (void *)&foo[20], sizeof(foo), compare_foos ); First, I think you want sizeof(foo_type) or sizeof(foo[0]). >void *beg, *end; >int nelements = (end - beg) / size; >I (naively?) thought that pointer arithmetic would work with "void *", and >that it would work in the same abstract units as "sizeof()", so that all >previous generic "char *" kludges could be replaced by "void *". The "abstract units" of sizeof() are "char" by definition. (Too many users have been assuming sizeof(char)==1, so it's official in C++ and ANSI C. I wish it had been measured in BITS in the first place!) Since "void *" means "pointer to object of unknown type", you can't do pointer arithmetic with it. What you really want is "((char *)end - (char *)beg) / size". (Don't use int or long!) Karl W. Z. Heuer (ihnp4!ima!haddock!karl), The Walking Lint