void pointers and more

V

Vivek

Hi,

I am trying to get as much information on void pointers. How do we take help
of void pointers in writing generic functions.

I have seen this kind of code for many years, but now I have got a chance to
write some myself.

Could anybody point me to a link where I can get more info for the same, or
refer me a book having more detail info.

Also if anybody has read the boot "Practicalk programming in C"
Orielly...give me some idea of how the book is.
I plan to buy it today. :)

Thanks a lot.
Vivek
 
D

Dan Pop

In said:
I am trying to get as much information on void pointers. How do we take help
of void pointers in writing generic functions.

We don't. They're only useful for functions expecting or returning
generic memory addresses. See the mem* functions from <string.h> and
malloc and friends.

It is fairly unusual to use them in other contexts: you can neither
dereference them nor perform arithmetic on them. Their sole merit is
that other pointer types are automatically converted to/from void
pointers when needed, without requiring a cast.

Dan
 
J

James Hu

We don't. They're only useful for functions expecting or returning
generic memory addresses. See the mem* functions from <string.h> and
malloc and friends.

Don't forget qsort and bsearch.
It is fairly unusual to use them in other contexts: you can neither
dereference them nor perform arithmetic on them. Their sole merit
is that other pointer types are automatically converted to/from void
pointers when needed, without requiring a cast.

This statement is accurate, but it does not imply that their use is
unusual. And while it may be their sole merit, it is useful in the
"generic" context if we take a guess at what the original poster means
by that term.

Functions like qsort and bsearch are generic, relying on size
information and a helper comparison function to take care of any thing
that would normally require specific type information.

In general, when designing a generic interface in C, the interface will
require the client of the interface to provide enough extra information
along with the pointer to do anything that requires type specific
information.

-- James
 
C

CBFalconer

Dan said:
We don't. They're only useful for functions expecting or
returning generic memory addresses. See the mem* functions from
<string.h> and malloc and friends.

It is fairly unusual to use them in other contexts: you can
neither dereference them nor perform arithmetic on them. Their
sole merit is that other pointer types are automatically
converted to/from void pointers when needed, without requiring
a cast.

What Dan fails to make clear above is their utility in allowing
pointers to be passed through code that does not care about the
actual type. The usage in the compare function passed through
qsort is typical. The caller of qsort, and the specified compare
function know what the actual pointer type is. All other routines
simply pass the pointers on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top