By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 2,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

const void *

P: n/a
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?

--
conrad

Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
co****@lawyer.com wrote:
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.

--
Clark S. Cox III
cl*******@gmail.com
Sep 20 '06 #2

P: n/a
co****@lawyer.com wrote:
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
mf*****@sbcglobal.net

Sep 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.