Sorting the Input

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

    Sorting the Input

    On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote:
    No. A function takes what it must due to its design.
    get_single_word () needs a char ** because its job is to set a char *
    that "does not belong to it". It is passed a pointer to the char *
    that is must set: get_single_word (char **);
    >
    Giving get_input() a char *** parameter does not mean that this must be
    passed to get_single_word . You get to say what you pass!

    An abstract overview of my own program by Ben had helped me focus on the
    design issue first. I came to know that whole problem went into the wrong
    direction as compared to what was intended. I think I am working on this
    program from last 1 month and it is still not done, Here is the intent:


    Program will ask the user for input and then will sort the input
    alphabetically and will print it on stdout

    Since I did not want to put any limitation on input size in this
    program, I have used dynamic memory allocation. My intent in
    creating and then solving this problem was purely of learning
    about dynamic memory allocation and C (as defined by ANSI
    standard) and nothing else. Regarding storing input words, since
    there is no agreed definition of what a word is, I have taken a
    very simple approach to it:

    "Any contiguous collection of characters, except single or
    multiple whitespace, is a word"


    As usual in C, each word is stored as an array of characters. To
    sort different words we will create another, second array. The
    elements of this array will be pointing to each input word and
    since each input word is an array, we will be pointing to a
    pointer hence this second array will have pointers to pointers as
    its elements. Then we will sort the pointers to point to
    different words (arrays). This Efficient technique is described
    in K&R2, as we are sorting the pointers to arrays, rather than
    arrays themselves.




    So this array of pointers will be created in main() but since we have no
    idea of how many words user will enter, we will be realloc()ing in calling
    function passing this array of pointers as argument. Or we can make this
    array a global array and then pass it as desired without messing with
    function arguments. Which one will be better ?







    --

    my email is @ the above blog.
    Gooogle Groups is Blocked. Reason: Excessive Spamming

  • Nick Keighley

    #2
    Re: Sorting the Input

    On 30 Sep, 07:39, arnuld <sunr...@invali d.addresswrote:
    On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote:
    No.  A function takes what it must due to its design.
    get_single_word () needs a char ** because its job is to set a char *
    that "does not belong to it".  It is passed a pointer to the char *
    that is must set: get_single_word (char **);
    >
    Giving get_input() a char *** parameter does not mean that this must be
    passed to get_single_word .  You get to say what you pass!
    >
    An abstract overview of my own program by Ben had helped me focus on the
    design issue first. I came to know that whole problem went into the wrong
    direction as compared to what was intended. I think I am working on this
    program from last 1 month and it is still not done, Here is the intent:
    >
             Program will ask the user for input and then will sortthe input
             alphabetically and will print it on stdout
    >
             Since I did not want to put any limitation on input size in this
             program, I have used dynamic memory allocation. My intent in
             creating and then solving this problem was purely of learning
             about dynamic memory allocation and C (as defined  by ANSI
             standard) and nothing else. Regarding storing input  words, since
             there is no agreed definition of what a word is,  I have taken a
             very simple approach to it:
    >
             "Any contiguous collection of characters, except single or
             multiple  whitespace, is a word"
    >
             As usual in C, each word is stored as an array of characters. To
             sort different words we will create another, second array. The
             elements of this array will be pointing to each input word and
             since each input word is an array, we will be pointingto a
             pointer hence this second array will have pointers to pointers as
             its elements. Then we will sort the pointers to point to
             different words (arrays). This Efficient technique is described
             in K&R2, as we are sorting the pointers to arrays, rather than
             arrays themselves.
    >
    So this array of pointers will be created in main() but since we have no
    idea of how many words user will enter, we will be realloc()ing in calling
    function passing this array of pointers as argument. Or we can make this
    array a global array and then pass it as desired without messing with
    function arguments. Which one  will be better ?
    as a rule of thumb I avoif global data. Pass parameters to suitable
    funtions. if the ptr-to-ptr-to-ptr confuses you consider
    a struct

    struct Word_table
    {
    int word_count;
    char **word_list;
    };


    --
    Nick Keighley

    "Never go in against a Sicilian when death is on the line"

    Comment

    Working...