Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sorting the input

Reply
Thread Tools

sorting the input

 
 
santosh
Guest
Posts: n/a
 
      04-28-2008
Richard Heathfield wrote:

> arnuld said:
>
>>> On Sun, 27 Apr 2008 05:24:25 +0000, Richard Heathfield wrote:

>>
>>
>>> When you're sorting an array of strings, each element is either a
>>> pointer to char or an array of char that effectively decays into a
>>> pointer to char. The qsort function will pass to your comparison
>>> function the address of that pointer - i.e. a pointer to pointer.

>>
>>
>> without confusing the idea I can simply say that an array of strings
>> is
>> *always* an array of pointer to chars. ok ?

>
> No, it's always an array of strings. It needn't be an array of
> pointers to char, though:
>
> char notanarrayofpointers[][6] =
> {
> "Apple",
> "Busby",
> "Carol",
> "Dough",
> "Earth",
> "Flood",
> "Grate",
> "Hotel"
> };
>
> This is an array of strings, but not an array of pointers to char.


So is this an "array of string" or perhaps an array of type string?

char arr_of_str[] = "hello";

Or is the above, and your example, just arrays which happen to contain
strings. Since strings are not really types in C can we even say "...
of strings" at all?

<snip>

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      04-28-2008
arnuld wrote:

>> On Mon, 28 Apr 2008 06:31:02 +0000, Richard Heathfield wrote:

>
>
>> No, it's always an array of strings. It needn't be an array of
>> pointers to char, though:
>>
>> char notanarrayofpointers[][6] =
>> {
>> "Apple",
>> "Busby",
>> "Carol",
>> "Dough",
>> "Earth",
>> "Flood",
>> "Grate",
>> "Hotel"
>> };
>>
>> This is an array of strings, but not an array of pointers to char.

>
> you mean it will not be represented as <arrays of pointers to chars>
> like when I give it to a function as argument ?


This declares a two-dimensional char array; an array N of array 6 of
char where N is determined by the compiler when is parses the
declaration. It notes that in this particular case eight initialisers
are used and hence the N is set as eight. Then each of the arrays from
0 to 7 are initialised by copying the respective string literals into
them.

<snip>

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      04-28-2008
> On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote:


> Given the correct call foo(notanarrayofpointers), we can deduce that foo
> takes as its parameter a char (*)[6], NOT a char **.


I did not know that passing a 2D array to a function results in <pointer
to whole array>. Thanks for telling me this C feature




--
http://lispmachine.wordpress.com/
my email ID is at the above address

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-28-2008
arnuld wrote:

> > On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote:

>
>
> > Given the correct call foo(notanarrayofpointers), we can deduce
> > that foo takes as its parameter a char (*)[6], NOT a char **.

>
> I did not know that passing a 2D array to a function results in
> <pointer to whole array>. Thanks for telling me this C feature
>


The implicit conversion for an array gives you a pointer to the first
element. The first element is an array of char of size 6, so the array
conversion is to a pointer to "array six of char". That's what Richard
showed you above.



Brian
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-29-2008
arnuld <(E-Mail Removed)> writes:
>> On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote:
>> Given the correct call foo(notanarrayofpointers), we can deduce that foo
>> takes as its parameter a char (*)[6], NOT a char **.

>
> I did not know that passing a 2D array to a function results in <pointer
> to whole array>. Thanks for telling me this C feature


Not to the whole array, just to the first element.

This is nothing more than one case of the more general rule that an
expression of array type is, in most contexts, converted to a pointer
to its first element (the exceptions are when the array expression is
(a) the operand of a unary "&", (b) the operand of "sizeof", or (c) a
string literal in an initializer, used to initialize an array object).

A 2D array is nothing more than an array of arrays. Everything about
2D arrays (syntax, semantics, whatever) follows directly from the
rules for arrays in general.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      05-12-2008
On Sun, 27 Apr 2008 05:24:25 +0000, Richard Heathfield
<(E-Mail Removed)> wrote:

> > Richard Heathfield wrote:
> >> CBFalconer said:


> >>> int p_strcmp(const void *pv1, const void *pv2) {
> >>> const char *v1 = pv1, *v2 = pv2;
> >>>
> >>> return strcmp(v1, v2);
> >>> }
> >>
> >> If you were planning to pass that to qsort, forget it.


> When you're sorting an array of strings, each element is either a pointer
> to char or an array of char that effectively decays into a pointer to
> char. The qsort function will pass to your comparison function the address
> of that pointer - i.e. a pointer to pointer.
>

If you have, as the OP did, array of pointer to string (which is
pointer to char, or often better const char) then the compare
arguments are actually pointer to pointer to char, disguised as
pointer to void, and this is indeed correct:

> #include <string.h>
> int p_strcmp(const void *pv1, const void *pv2)
> {
> char * const * v1 = pv1;
> char * const * v2 = pv2;
> return strcmp(*v1, *v2);
> }


But if you have array of array of char, there is no decay. The compare
arguments are pointer to string disguised as void*, and CBF's compare
routine is right. In fact, with the 'same representation' requirement
for /*flavored?*/char* and void*, it should work to simply give strcmp
directly to qsort. (Although I would call this a bad habit to get
into.) Also if you have array of struct whose first field is array of
char, and is the sort key, which is a case I have encountered.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
Sorting the Input arnuld C Programming 1 09-30-2008 07:47 AM
sorting the input arnuld C++ 16 09-25-2008 04:44 PM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
sorting by multiple criterias (sub-sorting) Tom Kirchner Perl Misc 3 10-11-2003 05:16 PM



Advertisments