Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15172

Re: Is there any way to refer to another array in qsort's compare function?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Dr Nick <3-nospam@temporary-address.org.uk>
Newsgroups comp.lang.c
Subject Re: Is there any way to refer to another array in qsort's compare function?
Date Thu, 29 Dec 2011 12:37:26 +0000
Lines 64
Message-ID <8739c3eepl.fsf@temporary-address.org.uk> (permalink)
References <f7d05964-b7de-45b1-afde-7ac9d7c38e9b@x34g2000prb.googlegroups.com> <0.7769aa1f0654775b9623.20111229115814GMT.87ehvnd1yh.fsf@bsb.me.uk> <17404410.1167.1325161235089.JavaMail.geo-discussion-forums@prmw6>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace individual.net 1QP60uze+RgL0LtDVap54AlK4Z16G+wfyoJ/FP33ZTNMVZhjap
Cancel-Lock sha1:KmKpmPG+6a8cUzYzeoNMtmBYv4M= sha1:6Tw5+l17Zc1IgzE+AUc2KeDgL5k=
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Xref x330-a1.tempe.blueboxinc.net comp.lang.c:15172

Show key headers only | View raw


88888 Dihedral <dihedral88888@googlemail.com> writes:

> Ben Bacarisse於 2011年12月29日星期四UTC+8下午7時58分14秒寫道:
>> Stone <ston...@gmail.com> writes:
>> 
>> > Here's the problem.
>> >
>> > I wrote a function 'int my_compare(const void *, const void *);' and
>> > passed it to qsort() of stdlib.h, in order to sort an array named
>> > *ptr* which contains indices of another array *data*. By that I
>> > intended to sort elements of *ptr* (i.e. ptr[i]) using data[ptr[i]] as
>> > the key value.
>> >
>> > However, the *data* array was defined as static in main(), and seemed
>> > not easy to access in my_compare(). Is there any way to achieve this?
>> 
>> You can either move the array so that it is defined at file scope
>> (i.e. outside main) or you can arrange for there to be a file-scope
>> pointer to it:
>> 
>>   #include <stdlib.h>
>>   #include <stdio.h>
>>    
>>   int *aux_sort_ptr;
>>    
>>   int my_compare(const void *p1, const void *p2)
>>   {
>>       const int *ip1 = p1, *ip2 = p2;
>>       return aux_sort_ptr[*ip1] - aux_sort_ptr[*ip2];
>>   }
>>    
>>   int main(void)
>>   {
>>       static int data[] = { 3, 1, 2 };
>>       int idx[] = { 1, 2, 3 };
>>       aux_sort_ptr = data;
>>       qsort(idx, sizeof idx/sizeof *idx, sizeof *idx, my_compare);
>>       for (int i = 0; i < sizeof idx/sizeof *idx; i++)
>>            printf("idx[%d] = %d, data[%d] = %d\n",
>>                   i, idx[i], idx[i], data[idx[i]]);
>>   }
>> 
>> This is ugly (and problematic in threaded code), but there's no elegant
>> way round it using standard C.  Many systems have other sort functions
>> whose comparison function can be handed another void * (see Dr Nick's
>> reply) but I don't think and have become common enough to though of as
>> "almost standard".
>
> This method uses more memory. 

One pointer's worth.

> Do you think that just 
> overloading the comparison function by OOP can make it not choking at all?

Why do you say that "Do you think that just overloading the comparison
function by OOP can make it not choking at all?"

To Stone: No-one has a clue what 88888 Dihedral is on about.  Many of us
suspect him to be a poor attempt to pass the Turing test.  Whatever he
is, don't worry about his comments.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is there any way to refer to another array in qsort's compare function? Stone <stonekyx@gmail.com> - 2011-12-29 00:27 -0800
  Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 08:57 +0000
  Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 11:58 +0000
    Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 04:20 -0800
      Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 12:37 +0000
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 04:48 -0800
          Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 12:59 +0000
            Re: Is there any way to refer to another array in qsort's compare function? Geoff <geoff@invalid.invalid> - 2011-12-29 10:09 -0800
              Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 17:50 -0800
                Re: Is there any way to refer to another array in qsort's compare function? Geoff <geoff@invalid.invalid> - 2011-12-29 19:29 -0800
                Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 00:36 -0800
                Re: Is there any way to refer to another array in qsort's compare function? Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-30 00:52 -0800
                Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 02:16 -0800
              Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 18:01 -0800
            Re: Is there any way to refer to another array in qsort's compare function? James Kuyper <jameskuyper@verizon.net> - 2011-12-29 13:21 -0500
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 23:18 -0800
    Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-07 23:40 -0800
      Re: Is there any way to refer to another array in qsort's compare function? pete <pfiland@mindspring.com> - 2012-01-08 07:26 -0500
        Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-08 12:50 +0000
          Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-08 06:02 -0800
        Re: Is there any way to refer to another array in qsort's compare function? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-01-17 12:35 +0200
  Re: Is there any way to refer to another array in qsort's compare function? Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-29 05:07 -0800
    Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 14:16 +0000
      Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 15:29 +0000
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 09:33 -0800
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 15:40 -0800
  Re: Is there any way to refer to another array in qsort's compare function? James Kuyper <jameskuyper@verizon.net> - 2011-12-29 10:50 -0500
  Re: Is there any way to refer to another array in qsort's compare function? pete <pfiland@mindspring.com> - 2012-01-07 20:50 -0500

csiph-web