qsort question

J

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
 
A

Alex

Joe said:
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
 
I

Ian Woods

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
 
O

osmium

Joe said:
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?
 
P

Paul Hsieh

Joe said:
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.
 
B

Ben Pfaff

Joe said:
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.
 
J

Jack Klein

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.
 
P

Peter Shaggy Haywood

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!
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"?
 
P

Paul Hsieh

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top