473,657 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is const necessary in eg int compar(const void *, const void *)

Is the keyword const necessary in the comparison function in qsort and
bsearch?

int (*compar)(const void *, const void *)

If the pointer cannot be dereferenced why worry if the pointed object
will be modified?
Oct 15 '08 #1
28 2636
lo************* **@gmail.c0m wrote:
Is the keyword const necessary in the comparison function in qsort and
bsearch?

int (*compar)(const void *, const void *)

If the pointer cannot be dereferenced why worry if the pointed object
will be modified?
Why can't the pointer be dereferenced?

--
Ian Collins
Oct 15 '08 #2
"lovecreatesbea ...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
Is the keyword const necessary in the comparison function
in qsort and bsearch?
Not strictly necessary, but prudent.
int (*compar)(const void *, const void *)

If the pointer cannot be dereferenced why worry if the
pointed object will be modified?
When the pointer(s) are converted to the appropriate object
type, they certainly can be dereferenced, and usually are.

--
Peter
Oct 15 '08 #3
On Oct 15, 10:55 am, Peter Nilsson <ai...@acay.com .auwrote:
"lovecreatesbea ...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
Is the keyword const necessary in the comparison function
in qsort and bsearch?

Not strictly necessary, but prudent.
int (*compar)(const void *, const void *)
If the pointer cannot be dereferenced why worry if the
pointed object will be modified?

When the pointer(s) are converted to the appropriate object
type, they certainly can be dereferenced, and usually are.
Thank you.

I ignored this, the const qualifier on the original pointers will
still require the same qulifier to be applied on those appropriate
object.
Oct 15 '08 #4
lo************* **@gmail.c0m said:
Is the keyword const necessary in the comparison function in qsort and
bsearch?

int (*compar)(const void *, const void *)
Yes.
If the pointer cannot be dereferenced why worry if the pointed object
will be modified?
The pointer *can* be dereferenced after conversion to the appropriate type.
For example, look at all the dereferencing going on here:

#include <time.h>

int cmp_tm(const void *vleft, const void *vright)
{
const struct tm *left = vleft;
const struct tm *right = vright;
int diff =
(left->tm_year right->tm_year) - (left->tm_year < right->tm_year);
if(0 == diff)
{
diff =
(left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
}
if(0 == diff)
{
diff =
(left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
}
/* hour, min, sec similarly */

return diff;
}
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '08 #5
On Oct 15, 11:16*am, Richard Heathfield <r...@see.sig.i nvalidwrote:
lovecreatesbea. ..@gmail.c0m said:
If the pointer cannot be dereferenced why worry if the pointed object
will be modified?

The pointer *can* be dereferenced after conversion to the appropriate type.
For example, look at all the dereferencing going on here:
Yes, thanks. I got it.
* if(0 == diff)
* {
* * diff =
* * (left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
* }
* if(0 == diff)
* {
* * diff =
* * (left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
* }
why don't you put your code together and create another same selection
block :)
Oct 15 '08 #6
lo************* **@gmail.c0m said:
On Oct 15, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>lovecreatesbea ...@gmail.c0m said:
If the pointer cannot be dereferenced why worry if the pointed object
will be modified?

The pointer *can* be dereferenced after conversion to the appropriate
type. For example, look at all the dereferencing going on here:

Yes, thanks. I got it.
>if(0 == diff)
{
diff =
(left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
}
if(0 == diff)
{
diff =
(left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
}

why don't you put your code together and create another same selection
block :)
I haven't the faintest idea what you're talking about.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '08 #7
"lo************ ***@gmail.c0m" <lo************ ***@gmail.comwr ites:
On Oct 15, 10:55 am, Peter Nilsson <ai...@acay.com .auwrote:
>"lovecreatesbe a...@gmail.c0m" <lovecreatesbea ...@gmail.comwr ote:
Is the keyword const necessary in the comparison function
in qsort and bsearch?

Not strictly necessary, but prudent.
int (*compar)(const void *, const void *)
If the pointer cannot be dereferenced why worry if the
pointed object will be modified?

When the pointer(s) are converted to the appropriate object
type, they certainly can be dereferenced, and usually are.

Thank you.

I ignored this, the const qualifier on the original pointers will
still require the same qulifier to be applied on those appropriate
object.
Hmm, but wait a minute. I think your original point was valid.

For a function like

void foo(const int *p) { /* ... */ }

the compiler will complain if you attempt to assign to, or otherwise
modify, *p. So the compiler helps you make good on the promise you're
making to the caller, that *p will not be modified; this is the whole
point of const. You could circumvent this by assigning to *(int *)p,
but at least the fact that you had to introduce a cast serves as warning
that you might be doing something wrong.

But for a function like

void bar(const void *p) { /* ... */ }

where p is known to point to an int, you *have* to cast p before you can
do much with it. And if you're going to cast it, you could just as
easily cast it to `int *' as to `const int *', and the compiler will (in
my tests) remain silent either way. So the help you get from using
const is much less.

Now if we also have

void qux(void *);

and `bar' attempts to call `qux(p)', the compiler does whine. (This in
fact is exactly the whine that occurs if you pass a function taking
`void *' arguments to `qsort'.) But certainly a lot of the protection
has been lost. One could argue that this makes the use of
`const void *' sort of pointless, but if nothing else it provides some
documentation, I suppose.
Oct 15 '08 #8
Nate Eldredge said:

<snip>
But for a function like

void bar(const void *p) { /* ... */ }

where p is known to point to an int, you *have* to cast p before you can
do much with it.
Not so:

#include <stdio.h>

void bar(const void *p)
{
const int *pi = p;
printf("%d\n", *pi);
} /* look, ma, no casts */

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 15 '08 #9
lo************* **@gmail.c0m wrote:
On Oct 15, 11:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>lovecreatesbea ...@gmail.c0m said:
>>If the pointer cannot be dereferenced why worry if the pointed object
will be modified?
The pointer *can* be dereferenced after conversion to the appropriate type.
For example, look at all the dereferencing going on here:

Yes, thanks. I got it.
> if(0 == diff)
{
diff =
(left->tm_mon right->tm_mon) - (left->tm_mon < right->tm_mon);
}
if(0 == diff)
{
diff =
(left->tm_mday right->tm_mday) - (left->tm_mday < right->tm_mday);
}

why don't you put your code together and create another same selection
block :)
If the value of diff was 0 at the first if(), then diff gets
recalculated, in which case it might be non-0 for the second if(), so
you can't combine the two blocks into a single if() block.
Oct 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2291
by: john smith | last post by:
Hi, say I have a class like so: class A { public: virtual void foo(); void bar() const; }; so when I actually define foo() and bar(), do I have to re-type the virtual and const modifiers respectively? Is it more a matter of style?
12
1464
by: DeMarcus | last post by:
Hi, I have an object, let's call it HydraulicPress class HydraulicPress { public: void setIron( Iron* iron ) const; Iron* getIron( void ) const;
10
2278
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
1
3908
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
6
8223
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
16
3153
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
7
1886
by: Bala L | last post by:
I have a class with a private array data member 'm_array'. I have written a public function, called 'fileRead', to read values into the array from a file. I just noticed that I have declared this function to be const. I dont understand how the code compiled and executed without returning an error or a warning. I am assigning values from a file to m_array inside this function. I thought that a member function can be made const only if...
4
6682
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.