473,508 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

qsort question

Joe
Hi,

The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why isn't
the first argument to the qsort function declared as "const void *base" ?

Many thanks,
Joe Hesse
Nov 14 '05 #1
8 2049
Joe writes:
The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why isn't the first argument to the qsort function declared as "const void *base" ?


Because the array does, in fact, get destroyed?
Nov 14 '05 #2
"Joe" <jo*******@actcx.com> wrote in message
news:40*********************@newsreader.visi.com.. .
Since const is used in the declaration of the comparison function,
why isn't the first argument to the qsort function declared as
"const void *base" ?


Because the comparison function must not modify the elements, but qsort()
itself must (albeit only changing the order).

Alex
Nov 14 '05 #3
"Joe" <jo*******@actcx.com> wrote in
news:40*********************@newsreader.visi.com:
Hi,

The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const
void *e1, const void *e2));

Since const is used in the declaration of the comparison function, why
isn't the first argument to the qsort function declared as "const void
*base" ?

Many thanks,
Joe Hesse


The array of elements is modified, so it can't be const.
The comparison function shouldn't modify but merely compare, hence the
consts.

Ian Woods
Nov 14 '05 #4
"Joe" <jo*******@actcx.com> wrote:
The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why isn't
the first argument to the qsort function declared as "const void *base" ?


The purpose of qsort is to permute an array of void * pointers. The
reason its not const is because it *is going* to modify the result.
(While this rule of thumb is usually a good one to go by, it doesn't
have any meaning for the va_arg functions for some rather poor
reasons.)

The comp function has its arguments declared as const because it must
not modify the parameters as a side effect.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #5
qe*@pobox.com (Paul Hsieh) writes:
"Joe" <jo*******@actcx.com> wrote:
The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why isn't
the first argument to the qsort function declared as "const void *base" ?


The purpose of qsort is to permute an array of void * pointers. [...]


No, the purpose of qsort is to permute an array of some type
decided on by the user. A pointer-to-void is used to point to
the first element of this array.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Nov 14 '05 #6
On Mon, 19 Jan 2004 10:07:12 -0800, "osmium" <r1********@comcast.net>
wrote in comp.lang.c:
Joe writes:
The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why

isn't
the first argument to the qsort function declared as "const void *base" ?


Because the array does, in fact, get destroyed?


Er, um, hopefully not destroyed. Reordered in most cases, certainly.

After all, data can always be destroyed in an O(1) operation, and
sorting generally cannot be. So using a sort to destroy data would be
rather inefficient.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
Groovy hepcat Paul Hsieh was jivin' on 19 Jan 2004 13:28:45 -0800 in
comp.lang.c.
Re: qsort question's a cool scene! Dig it!
"Joe" <jo*******@actcx.com> wrote:
The declaration of qsort is:
void qsort(void *base, size_t count, size_t size, int (*comp)(const void
*e1, const void *e2));

Since const is used in the declaration of the comparison function, why isn't
the first argument to the qsort function declared as "const void *base" ?
The purpose of qsort is to permute an array of void * pointers. The


Hold it right there. You wanna think about that for a moment?
reason its not const is because it *is going* to modify the result.
(While this rule of thumb is usually a good one to go by, it doesn't
have any meaning for the va_arg functions for some rather poor
reasons.)


Huh? What va_arg functions? How do they relate to qsort()? And to
what rule of thumb are you refering?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #8
sh****@australis.net.STOP.SPAM (Peter "Shaggy" Haywood) wrote:
reason its not const is because it *is going* to modify the result.
(While this rule of thumb is usually a good one to go by, it doesn't
have any meaning for the va_arg functions for some rather poor
reasons.)
Huh? What va_arg functions?


vprintf, vsprintf, vsnprintf, vfprintf, vscanf. The all take va_list
parameters.
How do they relate to qsort()?
They don't.
[...] And to what rule of thumb are you refering?


That if you don't need to or shouldn't modify a parameter on the end
of a reference, then you can assume its declared with a const and
isn't modified. This is the case with the qsort callback function, so
the OP can deduce that its there and why its there. The point is that
this reasoning doesn't work for the v... functions -- they don't
declare the va_list as const, and there is apparently some hidden
thing in there that they might modify.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #9

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

Similar topics

11
2824
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
7
5283
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
7
7438
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
16
2673
by: t_pantel | last post by:
I 've got the following structure: typedef struct GROUPED { short val ; short code; short group; short forecast_cd; short double_ind; short min;
10
355
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
8
2333
by: Max | last post by:
Hi everybody, suppose you have to order a list of integers which refer to points located in the 3D space. The compare() function is based on the distance that these points have with respect to...
14
2565
by: subramanian100in | last post by:
What is meant by stable qsort ?
17
235
by: Ron Ford | last post by:
Is qsort an intrinsic for C? -- We must respect the other fellow's religion, but only in the sense and to the extent that we respect his theory that his wife is beautiful and his children smart....
61
5767
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
0
7115
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
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
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...
0
7489
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...
0
5624
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,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
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 ...
0
414
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...

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.