const void *

C

conrad

What purpose does qualifying
a pointer to void with const
really serve? Before you can use what
the pointer points to, you must cast
it to the appropriate pointer type.
Then, you may as well include the
const qualification in the case. Being that
the const qualification serves no point
upon casting the pointer to void to some
other pointer type.

For example, take a look at 'free'.
Why is the parameter type 'const void *'?
What purpose is it really serving?
 
C

Clark S. Cox III

What purpose does qualifying
a pointer to void with const
really serve? Before you can use what
the pointer points to, you must cast
it to the appropriate pointer type.

No, void pointers will be implicitly converted to other pointer types
with the same or greater cv qualifications. This means that the
following implicit conversions are valid:

void* -> const void*
void* -> char*
void* -> const char*
const void * -> const char*

while the following are not:

const void* -> void*
const void* -> char*
Then, you may as well include the
const qualification in the case. Being that
the const qualification serves no point
upon casting the pointer to void to some
other pointer type.

For example, take a look at 'free'.
Why is the parameter type 'const void *'?
What purpose is it really serving?

So you can pass a const pointer without a cast.
 
M

Mark F. Haigh

What purpose does qualifying
a pointer to void with const
really serve? Before you can use what
the pointer points to, you must cast
it to the appropriate pointer type.
Then, you may as well include the
const qualification in the case. Being that
the const qualification serves no point
upon casting the pointer to void to some
other pointer type.

For example, take a look at 'free'.
Why is the parameter type 'const void *'?
<snip>

But it isn't. Quoth ISO/IEC 9899:1999:

7.20.3.2 The free function

Synopsis

1 #include <stdlib.h>
void free(void *ptr);

What purpose is it really serving?

A const pointer as a function argument generally means something to the
extent of "This function promises not to modify what the pointer points
to."


Mark F. Haigh
(e-mail address removed)
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top