473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

const void *

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
2 10480
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
13
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
14
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.