home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: I wish ANSI-C sizeof (void) == 1 (was Re: preprocessing subterfuge)
- Message-ID: <1992Aug18.144833.7579@sq.sq.com>
- Summary: Disagree.
- Organization: SoftQuad Inc., Toronto, Canada
- References: <BswME5.6Mp@research.canon.oz.au> <1992Aug14.195132.16700@crd.ge.com> <Bt5n8A.41B@research.canon.oz.au>
- Date: Tue, 18 Aug 92 14:48:33 GMT
- Lines: 29
-
- >>> void * is the natural type to use when you're considering a
- >>> pointer to a chunk of memory that can hold any arbitrary type ...
- >>> And any time I need to do arithmetic on one, it's always in terms
- >>> of bytes ...
- >> If you need to do arithmetic on one, then the variable should not be
- >> declared as "void *" in the first place. That type is for pointing to
- >> objects whose size you don't know. ...
- > Yes, but I'm _not_ dealing with arrays of bytes. I'm pointing to arbitrary
- > locations which store as yet unknown types.
-
- The last two quoted posters are both correct. In ANSI C, it is specified
- that character types occupy one byte and that any object (i.e. a single
- declared variable, or the result of a single call to malloc()) is made up
- of bytes which can be (sorry, Steve) addressed linearly. If you really
- have to do operations of the type just described, then you ARE viewing the
- object as an array of bytes, and char * (or unsigned char *, etc.) is the
- right type to use. If you find this unpleasant, consider "typedef char byte".
-
- Void * points with the same precision as char *, but is appropriate when you
- will be treating the object of unknown type as a WHOLE rather than as an
- array of bytes. This is why it has ITS special properties: it can be freely
- converted without a cast to and from any other object pointer type, but you
- can't do address arithmetic on it.
- --
- Mark Brader "You can't [compare] computer memory and recall
- SoftQuad Inc., Toronto with human memory and recall. It's comparing
- utzoo!sq!msb, msb@sq.com apples and bicycles." -- Ed Knowles
-
- This article is in the public domain.
-