473,569 Members | 2,400 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 2053
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*******@actc x.com> wrote in message
news:40******** *************@n ewsreader.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*******@actc x.com> wrote in
news:40******** *************@n ewsreader.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*******@actc x.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*******@actc x.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********@com cast.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.l earn.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*******@actc x.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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #8
sh****@australi s.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
2831
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) becomes 11, then the program crashes. It is obviously qsort that may me overwritten the used memory location. The weird thing is that it is...
7
5287
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
7443
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
2681
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 there a bug in my code? Thanks... Original Array: 3.125420 8.618710
8
2338
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 the origin (0,0,0). So, using the standart qsort() function, I think this task should be accomplished as follows: qsort(int_vector,...
14
2569
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. 5 H. L. Mencken
61
5781
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 with this: void qsort(int v, int left, int right) { int i, last;
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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...
0
6287
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.