sorting the input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Default User

    #76
    Re: sorting the input

    arnuld wrote:
    On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote:
    >
    >
    Given the correct call foo(notanarrayo fpointers), 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

    Comment

    • Keith Thompson

      #77
      Re: sorting the input

      arnuld <NoSpam@NoPain. comwrites:
      >On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote:
      >Given the correct call foo(notanarrayo fpointers), 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_Keit h) <kst-u@mib.org>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • David Thompson

        #78
        Re: sorting the input

        On Sun, 27 Apr 2008 05:24:25 +0000, Richard Heathfield
        <rjh@see.sig.in validwrote:
        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.ne t

        Comment

        Working...