Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Jun 2002 20:49:18 -0400
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        Nicolas Rachinsky <list@rachinsky.de>, hackers@FreeBSD.ORG
Subject:   Re: sorting in C
Message-ID:  <20020615004918.F275ABB2C@i8k.babbleon.org>
In-Reply-To: <20020614223608.GA16612@pc5.abc>
References:  <20020614124530.GA16778@lnuxlab.ath.cx> <B92FB0C7.2549%dan@arlow.com> <20020614223608.GA16612@pc5.abc>

next in thread | previous in thread | raw e-mail | index | archive | help
On Friday 14 June 2002 06:36 pm, Nicolas Rachinsky wrote:
| * Dan Arlow <dan@arlow.com> [2002-06-14 14:40 -0400]:
| > dumb question: extra "&" in the previous post?
| >
| > why do both this
| >
| > > qsort(&array, NUM_INTS, sizeof(*array),
| > > (int (*)(const void *, const void *)) comp);
| >
| > and this
| >   qsort(array, NUM_INTS, sizeof(*array),
| >   (int (*)(const void *, const void *)) comp);
| >
| > work properly? I tried both and both compile/run/sort fine.
| >
| > (the difference afaik is that the first passes an *int[] aka int** and
| > the second passes an int[] aka int*)
|
| No, see http://www.eskimo.com/~scs/C-faq/q6.12.html

I think that probably doesn't clarify much.  (Indeed, in this case, it would 
seem to obfuscate the above question more than clarify since the question is 
"why are they the same" and the FAQ question is meant to explain why these 
two are *different* to somebody who expects them to be the *same.*)


It's because an array *is* a pointer to the first element, and &array is a 
pointer to the array, but the first element has the same address as the 
array, since the address of the array is the address of the first element.  
Thus, the code works as long as the compiler will accept it.

If qsort had a "typed" first element, then you'd get an error message from 
one or the other of them (though it would probably work), but as it happens, 
qsort declares the type to be void *, so it will take *any* pointer type. 
Thus, no complaint from the compiler.

|
| Nicolas
|
| To Unsubscribe: send mail to majordomo@FreeBSD.org
| with "unsubscribe freebsd-hackers" in the body of the message

-- 
Brian T. Schellenberger . . . . . . .   bts@wnt.sas.com (work)
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                        http://www.babbleon.org

http://www.eff.org                      http://www.programming-freedom.org 

If you smell the smoke you don't need to be told what you've got to do;
Yet there's a certain breed, so very in-between, they'd rather take a
vote.   -- DEVO  --  Here To Go

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020615004918.F275ABB2C>