Re: Any way to take a word as input from stdin ?



arnuld said:


Now since *my* definition of word is done.

Yes. Whitespace-delimited.

Here is the outline of the program:
[...]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void get_words( char** );
void sort_words( char** );
void printf_words( char** );

I don't think this is going to cut it.

You want to sort many words (i.e. more than one), so you will need to store
them, assuming you want an in-memory sort. The natural way to store them
(or at least, the natural way for me to store them) is by allocating a
number of pointers to char (which can be reallocated if it proves to be
insufficiently long), each of which points to a word. The pointers to char
will be stored in a dynamic array, the base element of which will be
pointed to by a char **. For get_words to be able to do the allocation and
any necessary reallocations, you must be able to modify this char **
within the get_words function. For this change to "stick" in the caller,
you will need either to pass a pointer to the char **, or return the char
** from the function. Thus, you will need, at a minimum (at this point),
either this:

char **get_words(void);

or this:

void get_words(char ***);

But you need to know how many words you've captured! So you must either
return the count or pass a pointer to an integer object in which to store
the count. So you'll need either this:

char **get_words(size_t *);

or this:

size_t get_words(char ***);

But what if something goes wrong? You'll need to be able to report an
error. The natural way to do this is via a return value, which means we
can't use that value for either the list or the count, and that leads us
to:

int get_words(char ***, size_t *);

Since they don't need to modify the caller's status, sort_words and
print_words can be of type int(char **, size_t).

<snip>

SOLUTION: pda is an array of pointers, where pointers are pointing to
different words input by the user ( which are in fact arrays of
characters terminated by null, which means they are string literals of C,
which means it is still inherently confusing to me )

Not string literals - just strings.

<snip>

3rd, we do have an idea on
the maximum length of the word.

The longest one you find. That's why you use dynamic allocation - it means
you can fit the longest word you find without having to worry about
wasting space catering for longer words.

<snip>

I will limit the longets
words to what we call "Longest word in Shakespeare's work" whihc is 27
characters long, hence limiting the array size to be used to store the
words to 28.

good idea ?

Up to you, but I wouldn't bother setting a limit (or, if I did, I'd set it
at a million or so, and treat any string longer than that as a reportable
error). With dynamic allocation, you don't /need/ to set a limit; you
simply allocate as you go, and reallocate if necessary.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: HeapFree() Failure
    ... After all, you are storing pointers to wchar_t, not wchar_t. ... globally defined strings it declares like so: ... Sixteen bytes are successfully allocated to store pointers ... //This memory allocation call will give me sixteen bytes to store four ...
    (microsoft.public.windowsce.embedded)
  • Re: Advice on how to return a list of values
    ... What is the lifetime of the pointers? ... structure of 64 char * pointers. ... allocation if its called very often. ... live lists in your program (let's not even start with ...
    (comp.lang.c)
  • Re: Can C do it ?
    ... I'd like to know if it would be possible to store constant string in ... I'd first like to avoid pointers so i can see how far we go ... char str; ... You can't store two characters in the space reserved for one... ...
    (comp.lang.c)
  • Re: Holiday Game
    ... That requires mangling your char (and void) pointers so that you can ... store the byte offset somewhere. ...
    (alt.lang.asm)
  • Re: Holiday Game
    ... That requires mangling your char (and void) pointers so that you can ... store the byte offset somewhere. ...
    (alt.lang.asm)