From owner-freebsd-hackers Fri Jun 14 17:49:31 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mail5.nc.rr.com (fe5.southeast.rr.com [24.93.67.52]) by hub.freebsd.org (Postfix) with ESMTP id 6E19B37B413 for ; Fri, 14 Jun 2002 17:49:27 -0700 (PDT) Received: from i8k.babbleon.org ([66.57.86.84]) by mail5.nc.rr.com with Microsoft SMTPSVC(5.5.1877.687.68); Fri, 14 Jun 2002 20:49:24 -0400 Received: by i8k.babbleon.org (Postfix, from userid 111) id F275ABB2C; Fri, 14 Jun 2002 20:49:18 -0400 (EDT) Content-Type: text/plain; charset="iso-8859-1" From: Brian T.Schellenberger To: Nicolas Rachinsky , hackers@FreeBSD.ORG Subject: Re: sorting in C Date: Fri, 14 Jun 2002 20:49:18 -0400 X-Mailer: KMail [version 1.3] References: <20020614124530.GA16778@lnuxlab.ath.cx> <20020614223608.GA16612@pc5.abc> In-Reply-To: <20020614223608.GA16612@pc5.abc> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <20020615004918.F275ABB2C@i8k.babbleon.org> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Friday 14 June 2002 06:36 pm, Nicolas Rachinsky wrote: | * Dan Arlow [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