Re: pointer arithmetic on a pointer to void

"James K. Lowden" <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
On Thu, 10 Oct 2013 17:47:43 -0500
"Craig A. Berry" <[email protected]> wrote:

> Since what we have now is dubious from a sC standards viewpoint,
> wouldn't it be better to just make it a pointer to char in the first
> place

Absolutely right on all counts.  The only valid uses of void* are 

1.  compare with NULL 
2.  convert to char*
3.  convert back to the type it points to

In olden times, as I'm sure you remember, the standard library declared
ordinary buffers to be char*, and the compiler never complained if you
happened to send int* (because, say, you're reading ints).  With the
introduction of functional prototypes in C89, that was no longer
possible because the compiler would not "convert" int* to char* for
you.  So ANSI invented void* to stand in for char*'s old role, albeit
in a more limited form.  char* retained its elevated status: anything
could be cast to and from char* in order to gain byte-addressability.  

Pointer arithmetic on void* is invalid for the perfectly good reason
that the void type has no size.  

The choice between redefining the pointer to char* or casting void* to
char* is one of convenience.  If your one-liner makes the compiler
happy, I'd say that's most convenient.  

Thanks for pointing it out.  I'm surprised it hasn't cropped up
before. 

--jkl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.