Sorting the Input

A

arnuld

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 ?
 
N

Nick Keighley

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 ?

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;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top